Here you can find the source of copyFile(String srcPath, String dstPath, boolean replace)
Parameter | Description |
---|---|
srcPath | String the source path (File) |
dstPath | String the destination path (File) |
replace | is dstPath exist, and this is true that file will be replaced with new content |
public static boolean copyFile(String srcPath, String dstPath, boolean replace)
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**//from w w w .j a v a 2 s.co m * copy a file * * @param srcPath * String the source path (File) * @param dstPath * String the destination path (File) * @param replace * is dstPath exist, and this is true that file will be replaced with new content * @return true, if copy successfully */ public static boolean copyFile(String srcPath, String dstPath, boolean replace) { InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { int byteRead; File srcFile = new File(srcPath); File dstFile = new File(dstPath); // have but no replace if (dstFile.exists() && !replace) { return false; } // don't have, create new one if (!dstFile.exists()) dstFile.createNewFile(); if (srcFile.exists()) { // file exists inputStream = new FileInputStream(srcFile); fileOutputStream = new FileOutputStream(dstFile); byte[] buffer = new byte[1444]; while ((byteRead = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } fileOutputStream.close(); inputStream.close(); return true; } return false; } catch (Exception e) { System.out.println("copy file failed"); e.printStackTrace(); return false; } } }