你們在工做中有沒有遇到過這樣的狀況:你有一個文件夾裏面有各類各樣的文件夾,文件夾裏還有不少子文件夾和文件。可是你今天只想要裏面的某一種格式的全部文件。例如說:PPT,DOC。若是你喜歡花時間一個一個複製出來,哪你關了個人博客去複製吧,很顯然會耽誤你很長時間,甚至會想摔電腦。做爲一個程序猿,我最不能容忍的就是重複簡單操做,因此本身搞了個程序玩玩。java
public class getFile { List<File> Original_List=new ArrayList<>(); //save files in Original_path List<File> target_List=new ArrayList<>(); //save files will to target_path public void getFile(String Original_path,String target_path,String format1,String format2) { FileOutputStream fos=null; try { File dir = new File(Original_path); System.out.println("Read File....."); getAllFile(dir,Original_List); for (int i = 0; i < Original_List.size(); i++) { if (Original_List.get(i).getName().endsWith(format1)||Original_List.get(i).getName().endsWith(format2)) { target_List.add(Original_List.get(i)); } } for (File file : target_List) { try { fos = new FileOutputStream(target_path+file.getName()); Files.copy(Paths.get(file.toURI()),fos); } catch (IOException e) { System.err.println("copy Error"); e.printStackTrace(); }finally { fos.close(); System.out.println(file.getName()+" Succeed Copy!"); } } }catch(Exception k) { System.out.println("Original_path ERROR"); k.printStackTrace(); } } private static void getAllFile(File f,List<File> list) { File[] fList = f.listFiles(); for (File file : fList) { list.add(file); if (file.isDirectory()) { getAllFile(file,list); } } } }
public class ReadConfig { public static String Original_path; public static String Target_path; public static String format1; public static String format2; static { Properties properties = new Properties(); File file = new File("./config.properties"); FileInputStream fis = null; try { fis = new FileInputStream(file); properties.load(fis); } catch (IOException e) { e.printStackTrace(); throw new Error("讀取配置文件失敗!"); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } Original_path = properties.getProperty("Original_path"); Target_path= properties.getProperty("Target_path"); format1= properties.getProperty("format1"); format2= properties.getProperty("format2"); } public static void showConfig() { System.out.println("============================================"); System.out.println("Original_path: " + Original_path); System.out.println("Target_path: " + Target_path); System.out.println("format1: " + format1); System.out.println("format2: " + Original_path); System.out.println("============================================"); } public static void main(String[] args) { showConfig(); } }
Original_path=G:\\javaweb Target_path=G:\\javaWeb_ppt\\ format1=.doc format2=.docx
Original_path=G:\\javaweb git
Target_path=G:\\javaWeb_ppt\\
github
public class Run { static String Original_path=ReadConfig.Original_path; static String Target_path=ReadConfig.Target_path; static String format1=ReadConfig.format1; //Lowercase static String format2=ReadConfig.format2; //Uppercase public static void main(String[] args) { getFile g =new getFile(); g.getFile(Original_path, Target_path, format1,format2); } }