Java URL Download downloadURL(URL url, File file)

Here you can find the source of downloadURL(URL url, File file)

Description

Download the contents of the given URL to the given file

License

Open Source License

Parameter

Parameter Description
url The URL to download from
file The target file

Exception

Parameter Description
IOException if an error occurs

Declaration

public static void downloadURL(URL url, File file) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**/*from  ww  w. j  a va  2  s. co  m*/
     * Download the contents of the given URL to the given file
     * @param url The URL to download from
     * @param file The target file
     * @throws IOException if an error occurs
     */
    public static void downloadURL(URL url, File file) throws IOException {
        URLConnection conn = url.openConnection();
        InputStream stream = conn.getInputStream();
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = stream.read(buffer)) != -1) {
            fos.write(buffer, 0, read);
        }
    }

    /**
     * Utility method for quickly create a {@link BufferedReader} for
     * a given file.
     * @param file The file
     * @return the corresponding reader
     * @throws IOException if an error occurs
     */
    public static BufferedReader read(File file) throws IOException {
        return new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    }
}

Related

  1. downloadToFile(URL url, File file)
  2. downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
  3. downloadToString(String url)
  4. downloadURL(String addr, File outFile)
  5. downloadURL(String url, String outFile)
  6. downloadURL(URL url, String localPath)
  7. downloadUrlToFile(String surl, File file, String method)
  8. downloadUrlToFile(URL url, File file)
  9. downloadUrlToFile(URL url, File result)