生活随笔
收集整理的這篇文章主要介紹了
按文件夹名匹配并复制文件夹及子文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先說一下功能概要:
將文件或文件夾以關鍵字開頭的名字復制到另一個目錄下。
附加功能:定時檢測源目錄是否有新增文件或文件夾,并實時拷貝。
//拷貝文件/夾的方法public class CopyFolder {//是否是目標關鍵字文件夾private static boolean IS_AIM_FOLDER = false;/*** 分析:* A:封裝數據源File* B:封裝數據目的地File* C:判斷該File是文件夾還是文件* a:是文件夾* 就在目的地目錄創建該文件夾* 獲取該File對象下的所有文件或者文件夾File對象 遍歷得到每一個File對象* 回到C* b:是文件* 直接復制**/public static void main(String[] args) throws IOException {// 源路徑File srcFile = new File("Z:\\AA\\A\\B\\C");// 目標路徑File destFile = new File("Z:\\BB\\A\\B\\C");// 調用方法copyFolder(srcFile, destFile,destFile);}/*** 遞歸復制文件夾* @param srcFile* @param destFile* @param aimFile 僅提供對比* @throws IOException*/public static void copyFolder(File srcFile, File destFile,File aimFile) throws IOException {// 1.判斷File對象是否是文件夾if (srcFile.isDirectory()) {//2.判斷文件夾名稱是否包含關鍵字if(srcFile.getName().startsWith("運營發送")|| IS_AIM_FOLDER){IS_AIM_FOLDER = true;//3.包含則復制當前文件夾的所有文件// 拼接新文件夾所在路徑File newFolder = new File(destFile, srcFile.getName());// 創建文件夾newFolder.mkdir();// 遍歷源路徑下的所有File對象File[] fileArray = srcFile.listFiles();for (File file : fileArray) {// 遞歸調用copyFolder(file, newFolder, aimFile);}IS_AIM_FOLDER = false;}else{//4.不包含則向下遍歷// 遍歷源路徑下的所有File對象File[] fileArray = srcFile.listFiles();for (File file : fileArray) {// 遞歸調用copyFolder(file, destFile,aimFile);}}} else {//5.判斷是否屬于關鍵字目錄下的、是否包含關鍵字、是否還在當前文件夾的循環條件內、目標文件夾是否有改變。含任何一種條件都可復制if(srcFile.getName().startsWith("運營發送")||destFile.getName().startsWith("運營發送")|| IS_AIM_FOLDER ||!aimFile.equals(destFile) ){// 拼接新文件所在路徑File newFile = new File(destFile, srcFile.getName());// 調用方法,復制文件copyFile(srcFile, newFile);}}}// 高效字節流一次讀取一個字節數組復制文件public static void copyFile(File srcFile, File newFile) throws IOException, FileNotFoundException {// 數據源BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));// 目的位置BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));// 定義單次讀取字節數組的大小,一般就寫1024byte[] bys = new byte[1024];// read(byte[] bys)方法返回值為獲取到的字節個數,若沒有獲取到,則返回-1int length = 0;while ((length = bis.read(bys)) != -1) {// write(byte[] bys,int off,int length)方法指的是從指定字節數組的指定位置開始寫入(復制到)文件bos.write(bys, 0, length);}// 關閉輸出流bos.close();// 關閉輸入流bis.close();}
}
下面調用方法不能直接調上面的,需要將上面這段拷貝的代碼main方法改成普通方法,然后將類注入,在下面定時檢測時調用即可。
注意:下面的copyFolder.copyFunc(filePath,destFile)是調用的
//定時檢測方法private static String srcFile = "Z:\\AA\\A\\B\\C";private static String destFile = "Z:\\BB\\A\\B\\C";//啟動項目自動執行@PostConstructpublic String getFile() throws FileNotFoundException, IOException {final Timer timer = new Timer();String fileName1="";timer.schedule(new TimerTask() {public void run() {WatchKey key;try {WatchService watchService = FileSystems.getDefault().newWatchService();Paths.get(srcFile).register(watchService, StandardWatchEventKinds.ENTRY_CREATE);while (true) {File file = new File(srcFile);//path為監聽文件夾File[] files = file.listFiles();key = watchService.take();//沒有文件增加時,阻塞在這里for (WatchEvent<?> event : key.pollEvents()) {//獲取到剛傳的文件路徑String filePath = srcFile + "\\" + event.context();
// if (fileName.startsWith("運營發送")){
// Thread.sleep(1000);//休眠一秒copyFolder.copyFunc(filePath,destFile);
// }}if (!key.reset()) {break; //中斷循環}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, 2000, 3000);//第一個數字2000表示,2000ms以后開啟定時器,第二個數字3000,表示3000ms后運行一次runreturn "";}
以上代碼親測可用。
總結
以上是生活随笔為你收集整理的按文件夹名匹配并复制文件夹及子文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。