Scene: Copy all files in a folder to another folder
Code:
package cn.lj.java; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; public class FlowLogUpLoad { //文件夹的拷贝 public static void copyDir(String sourcePathDir, String newPathDir) { File start = new File(sourcePathDir); File end = newFile (newPathDir); String [] filePath = start.list (); // get the names of all files and directories under the folder of IF (! End.exists ()) { end.mkdir (); } boolean Flag = false ; for (the TEMP String: filePath) { // condition added to meet the circumstances of the IF ( new new file (sourcePathDir + + File.separator the TEMP) .isFile () && temp.endsWith (. "DAT" )) { // file the copy In Flag = copyFile (sourcePathDir the File.separator + + TEMP, the File.separator newPathDir + + TEMP); } IF (In Flag) { System.out.println ( "file:" + temp + "!, Copy success" ); } the else { System.out.println ( "file:" + temp + ", Copy failed!" ); } } } // copy of the document public static Boolean copyFile (the sourcePath String, String newPath) { Boolean in Flag = to false ; file ReadFile = new new file (the sourcePath); file newFile = new new file (newPath); BufferedWriter, BufferedWriter = null ; Writer writer = null; FileOutputStream fileOutputStream = null; BufferedReader bufferedReader = null; try{ fileOutputStream = new FileOutputStream(newFile, true); writer = new OutputStreamWriter(fileOutputStream,"UTF-8"); bufferedWriter = new BufferedWriter(writer); bufferedReader = new BufferedReader(new FileReader(readfile)); String line = null; while((line = bufferedReader.readLine()) != null){ bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } flag = true; } catch(IOException e) { flag = false; e.printStackTrace(); } finally { try { if(bufferedWriter != null){ bufferedWriter.close(); } if(bufferedReader != null){ bufferedReader.close(); } if(writer != null){ writer.close(); } if(fileOutputStream != null){ fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return flag; } public static void main(String[] args) { String sourcePath = "D:\\home\\before\\20200120"; String newPath = "E:\\home\\after\\20200120"; System.out.print("From:" + sourcePath); System.out.print("To:" + newPath); copyDir(sourcePath, newPath); } }