Java tutorial
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static void writeUrlToFile(String urlToRead, String folderToWrite, String fileName) throws MalformedURLException, IOException { URL urlIn = new URL(urlToRead); File folderOut = new File(folderToWrite); if (!(folderOut.exists() || folderOut.mkdirs())) { throw new RuntimeException("could not create folder " + folderToWrite); } File fileOut = new File(folderOut, fileName); try (InputStream in = new BufferedInputStream(urlIn.openStream()); OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));) { transfer(in, out); } } public static void transfer(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } public static void transfer(ReadableByteChannel in, WritableByteChannel out) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer); } buffer.clear(); } } public static void transfer(Reader in, StringBuilder out) throws IOException { char[] buffer = new char[4096]; int charsRead; while ((charsRead = in.read(buffer)) != -1) { out.append(buffer, 0, charsRead); } } }