Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is remotefile in JAVA?

Asked by Prateek Jain Oct 13, 2022 1.3K views 1 answer
Share

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);
    }
}


Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.