What is remotefile in JAVA?

622    Asked by pratee_8409 in Java , Asked on Oct 13, 2022

 I have this tiny class for downloading files from internet:

package com.github.coderodde.utils.io;

import java.io.IOException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.StandardCopyOption;

import java.util.Objects;


/**
 * This class implements a downloadable remote file.
 * 
 * @author Rodion "rodde" Efremov
 * @version 1.6 (Mar 14, 2020) ~ initial Happy Pi Day -version.
 * @since 1.6 (Mar 14, 2020)
 */
public class RemoteFile {


    /**
     * The URL of the target remote file.
     */
    private String url;
    /**
     * Constructs a new {@code RemoteFile} object with given URL as a string.
     * 
     * @param url the URL of the target remote file.
     */
    public RemoteFile(String url) {
        this.url = Objects.requireNonNull(url, "The URL is null.");
    }
    /**
     * Downloads the remote file to the local disk.
     * 
     * @param path the path of the target file on the local disk.
     * 
     * @throws MalformedURLException if there are problems with URL.
     * 
     * @throws IOException if I/O fails.
     */
    public void download(String path) throws MalformedURLException,
                                             IOException {
        InputStream inputStream = new URL(url).openStream();
        Files.copy(inputStream, 
                   Paths.get(path), 
                   StandardCopyOption.REPLACE_EXISTING);
    }
}


Answered by Preetam Shetty

It would probably be good to directly in the RemoteFile constructor perform the conversion to URL to allow the code to fail fast.


The code should also close the InputStream retrieved from openStream() once the copying is done, e.g. using a try-with-resources statement.

In general (unless required by your use case) it might be even better to directly use URL as type for the url parameter of the constructor and Path as type for the download(...) argument. This way your are handing off validation / parsing responsiblity to the caller.

And it might be good to make sure the path argument of download(...) is non-null before opening the InputStream.



Your Answer

Interviews

Parent Categories