Here you can find the source of copyFile(final File from, final File to)
public static void copyFile(final File from, final File to) throws IOException
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { static final int BUFFER = 8 * 1024; public static void copyFile(final File from, final File to) throws IOException { final InputStream inputStream = new BufferedInputStream(new FileInputStream(from)); copyFile(inputStream, to);// w w w .j a v a 2 s . c o m } public static void copyFile(final InputStream inputStream, final File to) throws IOException { createFile(to); OutputStream outputStream = null; try { copyStream(inputStream, outputStream = new BufferedOutputStream(new FileOutputStream(to))); } finally { inputStream.close(); if (outputStream != null) { outputStream.close(); } } } public static File createFile(final File file) throws IOException { if (!file.exists()) { createDirectoryRecursively(file.getParentFile()); file.createNewFile(); } return file; } public static int copyStream(final InputStream inputStream, final OutputStream outputStream) throws IOException { int result = 0; final byte[] buf = new byte[BUFFER]; for (;;) { final int numRead = inputStream.read(buf); if (numRead == -1) { break; } outputStream.write(buf, 0, numRead); result += numRead; } outputStream.flush(); return result; } public static boolean createDirectoryRecursively(File directory) { if (directory == null) { return false; } else if (directory.exists()) { return !directory.isFile(); } else if (!directory.isAbsolute()) { directory = new File(directory.getAbsolutePath()); } final String parent = directory.getParent(); if ((parent == null) || !createDirectoryRecursively(new File(parent))) { return false; } directory.mkdir(); return directory.exists(); } }