Save file from a URL in Java
Description
The following code shows how to save file from a URL.
Example
/*from ww w . j a v a2 s . c o m*/
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
InputStream in = null;
try {
URL u = new URL("http://www.java2s.com");
in = u.openStream();
for (int c = in.read(); c != -1; c = in.read()) {
System.out.write(c);
}
in.close();
} catch (MalformedURLException ex) {
System.err.println("not a URL Java understands.");
} finally {
if (in != null)
in.close();
}
}
}
The code above generates the following result.