java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to copy a URL into a file?

The code snippet below show you how to use the FileUtils.copyURLToFile(URL url, File destination) method of the Apache Commons IO library to help you to copy a URL directly into a file.

package com.javacoderanch.example.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class URLToFile {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.javacoderanch.org");
            File destination = new File("javacoderanch.html");

            //
            // Copy bytes from the URL to the destination file.
            //
            FileUtils.copyURLToFile(url, destination);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}