Here you can find the source of downloadURL(URL url, File file)
Parameter | Description |
---|---|
url | The URL to download from |
file | The target file |
Parameter | Description |
---|---|
IOException | if an error occurs |
public static void downloadURL(URL url, File file) throws IOException
//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))); } }