Here you can find the source of copyFile(File targetFile, File file)
public static void copyFile(File targetFile, File file)
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; public class Main { public static void copyFile(File targetFile, File file) { if (targetFile.exists()) { return; } else {//w w w. ja v a 2 s . c o m createFile(targetFile, true); } try { FileInputStream is = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(targetFile); byte[] buffer = new byte[1024 * 5]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } is.close(); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void createFile(String path, boolean isFile) { createFile(new File(path), isFile); } public static void createFile(File file, boolean isFile) { if (!file.exists()) { if (!file.getParentFile().exists()) { createFile(file.getParentFile(), false); } else { if (isFile) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } else { file.mkdir(); } } } } }