About this question
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);
}
}