2013-07-27 37 views
0

我一直在嘗試通過查找和替換名稱中的子字符串來重命名給定文件夾中的文件和文件夾。另外,文件的名稱也包含在它們的內容中。我需要將其替換爲新名稱。替換文件中包含的文件名,並用java的新名稱重命名文件

例如:中的所有文件和文件夾名稱

更改 「XXX」 到 「KKK」 和也在文件內容:

原始文件名:0001_XXX_YYY_ZZZ.txt

新文件名:0001_KKK_YYY_ZZZ.txt

以下是我使用的代碼。

當我運行下面的代碼而不調用函數replaceText()時,它重命名文件和文件夾。但是,當我嘗試更改文件的文本並重命名文件和文件夾時;文件的內容被改變,但文件和文件夾的重命名失敗。

請幫忙。看完後

public class FindReplaceAnywhere { 

    public static void main(String[] args) { 
     String find = "XXX"; 
     String replace = "KKK"; 
     String baseLoc = "D:\\0001_XXX_YYY_ZZZ"; 

     FindReplaceAnywhere obj = new FindReplaceAnywhere(); 

     File baseLocObj = new File(baseLoc); 
     LinkedList<File> baseFolderList = new LinkedList<File>(); 

     // Add base folder object to list 
     baseFolderList.add(baseLocObj); 

     // Get list of files in the folder 
     for(File file: baseLocObj.listFiles()) { 
      baseFolderList.add(file); 
     } 

     // Rename the files, folders & contents of files 
     obj.rename(baseFolderList, find, replace); 
    } 

    public void rename(LinkedList<File> fileList, String find, String replace) { 
     String tempStr = null; 
     int beginIndex = 0; 
     int endIndex = 0; 
     File tempFile; 

     System.out.println(">>> Batch Rename Process Begins >>>\n"); 

     for(File aFile:fileList) { 
      // If Object is File, change the text also 
      if(aFile.isFile()) { 
       replaceText(aFile,find,replace); 
      } 
     } 

     for(File aFile: fileList) { 
      System.out.println("Processing>>>"); 
      System.out.println(aFile.getPath()); 
      if(aFile.getName().contains(find)) { 
       // Get the name of File object 
       beginIndex = aFile.getPath().length() - aFile.getName().length(); 
       endIndex = aFile.getPath().length(); 
       tempStr = aFile.getPath().substring(beginIndex, endIndex); 
       tempStr = tempStr.replace(find, replace); 
      } 
      else { 
       System.out.println("Error: Pattern not found\n"); 
       continue; 
      } 
      tempFile = new File(aFile.getParentFile(),tempStr); 

      boolean success = aFile.renameTo(tempFile); 
      if(success) { 
       System.out.println("File Renamed To: "+tempFile.getName()); 
      } 
      else { 
       System.out.println("Error: Rename Failed\nPossible Cause: File is open in another application"); 
      } 
      System.out.println(""); 
     } 
    } 

    /** 
    * Replace the text of file if it contains filename 
    */ 
    public void replaceText(File file, String find, String replace) { 
     String fullText = ""; 
     String line = ""; 
     String fileName = ""; 
     String replaceName = ""; 

     BufferedReader in; 
     BufferedWriter out; 

     // Read the file contents 
     try { 
      in = new BufferedReader(new FileReader(file)); 
      while((line = in.readLine()) != null) { 
       fullText+=line+"\n"; 
      } 
     } 
     catch(FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 

     // Replace the text of file 
     fileName = file.getName().substring(0, file.getName().indexOf(".")); 
     replaceName = fileName.replace(find, replace); 
     fullText = fullText.replace(fileName, replaceName); 

     // Write the replaced text to file 
     try { 
      out = new BufferedWriter(new FileWriter(file)); 
      out.write(fullText); 
      out.close(); 
     } 
     catch(FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     }  
    } 
} 
+0

什麼是*確切*您所面臨的問題?只是說它不起作用是不夠的。 – gparyani

+1

你應該儘量減少代碼量,只提供相關部分,導致沒有人會像200行代碼一樣查看。另外指定你得到的錯誤和它發生的行。 – svz

+0

請參閱http://sscce.org/以獲取指導。 @svz它不一定是一個例外;這可能只是代碼沒有達到預期目的。那麼, – gparyani

回答

1

它看起來並不像你關閉你的輸入(in)文件,該文件認爲,文件打開 - 在* nix中的重命名應該仍然工作,但它會在Windows下失敗:

  • 使用finally塊來確保資源已關閉..但只有在您確信它已打開之後。

,而我在這,請允許我建議再變代碼:

  • 移動「聲明」以絕對的最後一個點在那裏他們可以使代碼..儘早申報。在這種情況下,inout都不必要地提前聲明。還有其他的;我會讓你解決的。

所以,輸入文件:

// Read the file contents 
    try { 
     BufferedReader in = new BufferedReader(new FileReader(file)); 
     // If you got this far, the file is open... 
     // use try/finally to ensure closure. 
     try { 
      while((line = in.readLine()) != null) { 
       fullText+=line+"\n"; 
      } 
     } 
     finally { 
      in.close(); 
     } 
    } 
    catch(FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    catch(IOException e) { 
     e.printStackTrace(); 
    } 

和輸出文件:

// Write the replaced text to file 
    try { 
     BufferedWriter out = new BufferedWriter(new FileWriter(file)); 
     try { 
      out.write(fullText); 
     } 
     finally { 
      out.close(); 
     } 
    } 
    catch(FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    catch(IOException e) { 
     e.printStackTrace(); 
    } 
+0

我從來不認爲它在Win和* nix上表現不同。這是什麼原因?找不到google .. – svz

+0

對於Unix:https://en.wikipedia。組織/維基/的inode#啓示;對於Windows,我只能分享我自己的經驗。易於驗證 - 在足夠大的文件上使用「更多」,並嘗試從另一個命令行程序中刪除它。 –

相關問題