Here you can find the source of download(URL url, File out)
public static void download(URL url, File out) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.net.*; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.file.*; import java.util.*; public class Main { public static void download(URL url, File out) throws IOException { File parent = out.getParentFile(); if (!out.exists()) { if (parent != null) parent.mkdirs();/*from w w w . ja va 2 s . c o m*/ File tempFile = File.createTempFile(UUID.randomUUID() .toString(), ".tmp", parent); tempFile.deleteOnExit(); try (InputStream is = url.openStream()) { ReadableByteChannel rbc = Channels.newChannel(is); FileOutputStream fos = new FileOutputStream(tempFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } Files.copy(tempFile.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING); tempFile.delete(); } } }