Here you can find the source of copyFile(File oldFile, File newFile)
public static void copyFile(File oldFile, File newFile) throws IOException
//package com.java2s; // Licensed under the Academic Free License version 3.0 import java.io.*; public class Main { /**/* w ww .ja v a2 s . c o m*/ * Copy file contents. */ public static void copyFile(File oldFile, File newFile) throws IOException { FileOutputStream out = null; FileInputStream in = null; try { out = new FileOutputStream(newFile); in = new FileInputStream(oldFile); byte[] buf = new byte[4096]; long size = oldFile.length(); long copied = 0; while (copied < size) { int n = in.read(buf, 0, buf.length); if (n < 0) throw new EOFException("Early EOF in input file"); out.write(buf, 0, n); copied += n; } } finally { if (in != null) try { in.close(); } catch (IOException e) { } if (out != null) try { out.close(); } catch (IOException e) { } } } /** * Read the specified number of bytes from the input * stream into a byte array. The stream is closed. */ public static byte[] read(InputStream in, long size) throws IOException { if (size < 0 || size > Integer.MAX_VALUE) throw new IOException("Invalid size " + size); int sz = (int) size; byte[] buf = new byte[sz]; int count = 0; while (count < sz) { int n = in.read(buf, count, sz - count); if (n < 0) throw new IOException("Unexpected EOF"); count += n; } in.close(); return buf; } }