Java tutorial
//package com.java2s; import java.io.BufferedInputStream; import java.io.File; 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.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.WRITE; public class Main { public static void writeUrlToFileNIO(String urlToRead, String folderToWrite, String fileName) throws MalformedURLException, IOException { URL urlIn = new URL(urlToRead); File folderOut = Paths.get(folderToWrite).toFile(); if (!(folderOut.exists() || folderOut.mkdirs())) { throw new RuntimeException("could not create folder " + folderToWrite); } Path pathOut = Paths.get(folderToWrite, fileName); try (ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(urlIn.openStream())); WritableByteChannel out = Files.newByteChannel(pathOut, CREATE, WRITE);) { 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); } } }