public class FileUtil { private static final int BYTE_SIZE = 1024; /** * 緩衝複製 * * @param sourceFile * 源文件絕對路徑 * @param targetFile * 目標文件絕對路徑 * @throws IOException */ public static void copFile(String sourceFile, String targetFile) throws IOException { FileInputStream fis = new FileInputStream(sourceFile); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(targetFile); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] b = new byte[BYTE_SIZE]; int len = 0; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } fos.close(); fis.close(); } /** * 刪除文件夾下全部文件--慎用 * * @param fileDir */ public static void deleteAll(File fileDir) { File[] files = fileDir.listFiles(); for (File file : files) { if (file.isDirectory()) { deleteAll(file); } else { file.delete(); } } fileDir.delete(); } public static void main(String[] args) throws IOException { // copFile("d:\\aaa\\AA.txt", "D:\\aaa\\BB.txt"); deleteAll(new File("D:\\aaa")); } }