2013-10-30 131 views
0

我是一位嘗試使用文件和目錄的Java初學者。我想創建一個程序,可以在搜索所有子目錄時自動更改文件名,以查找無效的文件名。實際上,我試圖將大量文​​件加載到服務器上,但服務器設置不允許包含特殊字符的文件名。首先我能寫在那裏,如果我通過了一個目錄路徑它重命名與在該目錄無效名稱的所有文件的代碼:在父目錄和子目錄中更改文件名

公共類重命名{

public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup"; 

public static void main(String[] args) { 

    //LinkedList<File> fileList = new LinkedList<File>(); 
    File obj = new File(baseLoc); 
    int count = 0; 

    for (File file: obj.listFiles()) 
    { 

     String origName = file.getName(); 

     if (origName.contains("&") || origName.contains("#") || origName.contains("@")) 
     { 
      System.out.println("Original name: "+origName); 
      origName = origName.replaceAll("&", "_and_"); 
      origName = origName.replaceAll("@", "_at_"); 
      String newName = origName.replaceAll("#", "_"); 
      System.out.println("New Name: "+newName); 
      String newLoc = baseLoc+"/"+newName; 
      File newFile = new File(newLoc); 
      System.out.println(file.renameTo(newFile)); 
      count++; 
     } 

    } 
} 

}

現在我想要做同樣的事情,但是隻有這一次,我希望即使在子目錄中重新命名所有文件。有人能指導我如何實現這一目標嗎?

+1

您可能需要使用遞歸 - 見http://stackoverflow.com/questions/189094/how-to-scan-a-folder-in-java – zencv

回答

1

遞歸是你的朋友

/**Removes 'invalid' characters (&,#,@) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/ 
public int renameDirectory(File base){ 
    //LinkedList<File> fileList = new LinkedList<File>(); 

    int count=0;//count the renamed files in this directory + its sub. You wanted to do this? 

    //Process each file in this folder. 
    for (File file: base.listFiles()){ 

     String origName = file.getName(); 
     File resultFile=file; 

     if (origName.contains("&") || origName.contains("#") || origName.contains("@")){ 
      //I would replace the if statement with origName.matches(".*[&#@].*") or similar, shorter but more error prone. 
      System.out.println("Original name: "+origName); 
      origName = origName.replaceAll("&", "_and_"); 
      origName = origName.replaceAll("@", "_at_"); 
      String newName = origName.replaceAll("#", "_"); 
      System.out.println("New Name: "+newName); 
      String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform. 
      File newFile = new File(newLoc); 
      System.out.println(file.renameTo(newFile)); 
      count++; 
      resultFile=newFile;//not sure if you could do file=newFile, tired 
     } 

     //if this 'file' in the base folder is a directory, process the directory 
     if(resultFile.isDirectory()){//or similar function 
      count+=renameDirectory(resultFile); 
     } 
    } 
    return count; 
} 
+0

謝謝。它幫助我理解文件功能。但是,我無法從main()傳遞baseLoc,並將其修改爲遞歸函數中的新位置。 – user1827582

+0

想通了。對不起,在一個微不足道的問題上感到困惑。謝謝你的幫助。 :) – user1827582

0

將您擁有的代碼移至實用方法(例如public void renameAll(File f){})。有一個條件,檢查文件是否是一個目錄,並遞歸調用你的方法與它的內容。之後,做你現在正在做的事情。

public void renameAll(File[] files){ 

    for(File f: files){ 
     if(f.isDirectory){ 
      renameAll(f.listFiles()); 
     } 
     rename(f); 

    } 

} 

public void rename(File f){ } 
相關問題