2016-12-07 55 views
-2

因此,我編寫了一個用於Unix機器上的小程序,該程序必須獲取存儲目錄中文件和文件夾的所有名稱,然後從中刪除所有字符他們。這些字符(或字符)將由用戶定義。用例:我把程序放在包含各種無用文件和目錄的目錄中,例如「NaCl2 !!!!!!!!!」,「H2O!」,「O2」和「Lithium !!!!! 「與我「問」它擺脫所有的劉海在所有directores的名字,因此會導致這樣的:刪除Java中目錄名稱中的某些字符

ls 
NaCl2 H2O O2 Lithium Unreal3.zip 

好吧,我想你明白這一點。所以這是代碼,它不能編譯(

DirRename.java:18: error: method renameTo in class File cannot be applied to given types; 
       tempDir.renameTo(name); 

)。我想這個錯誤是由我的代碼中的一個重大問題引起的。有沒有辦法讓它工作,你能告訴我嗎?

import java.io.*; import java.util.Scanner; 
class DirRename { 
    public static void main(String[] s) { 
     //DECLARING 
     String name, curDir, annoyngChar; 
     Scanner scan = new Scanner(System.in); 
     //WORKING 
     curDir = System.getProperty("user.dir"); 
     File dir = new File(curDir); 
     File[] listOfFiles = dir.listFiles(); 
     System.out.print("Type a character (or a line of them) that you want to remove from directories' names:"); 
     annoyngChar = scan.nextLine(); 
     System.out.println("\nAll directories will get rid of " + annoyngChar + " in their names."); 
     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isDirectory()) { 
       File tempDir = listOfFiles[i]; 
       name = tempDir.getName().replaceAll(annoyngChar, ""); 
       tempDir.renameTo(name); 
      } 
     }      
    } 
} 

需要說的是,該程序是未完成的,我很抱歉。

+0

可能重複的[如何重命名現有文件?](http://stackoverflow.com/questions/14970110/how-to-rename-an-existing-file) – Joe

+0

不現實,因爲我不只是想要重命名一個文件,我希望程序找到所有必須重命名的目錄(它們應該包含字符或程序要求我輸入的一行),然後通過刪除所有「非法」來重命名它們「字符(在上面提到的)。 –

+0

更改'tempDir.renameTo(name);'到'System.println(「name =」+ name)'確認新名稱是正確的,然後RTFM用於'File.renameTo()' – John3136

回答

0

File.renameTo(File dest)需要File參數,而不是String。因此,您需要使用正確的路徑(使用新名稱)創建File實例,並將該實例傳遞給renameTo

試試這個(未測試):

import java.io.*; import java.util.Scanner; 
class DirRename { 
    public static void main(String[] s) { 
     //DECLARING 
     String name, curDir, annoyngChar; 
     Scanner scan = new Scanner(System.in); 
     //WORKING 
     curDir = System.getProperty("user.dir"); 
     File dir = new File(curDir); 
     File[] listOfFiles = dir.listFiles(); 
     System.out.print("Type a character (or a line of them) that you want to remove from directories' names:"); 
     annoyngChar = scan.nextLine(); 
     System.out.println("\nAll directories will get rid of " + annoyngChar + " in their names."); 
     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isDirectory()) { 
       File f = listOfFiles[i]; 
       String oldName = f.getName(); 
       name = oldName.replaceAll(annoyngChar, ""); 
       if (!oldName.equals(name)) { 
        File newF = new File(dir, name); 
        f.renameTo(newF); 
       } 
      } 
     }      
    } 
} 

或者,您可以使用Files.move重命名文件。該文檔提供了一個如何重命名文件並將其保留在同一目錄中的示例。

+0

它沒有工作。或者我只是很愚蠢。 –

+0

查看我更新的答案。 – cybersam

+0

非常感謝你@cybersam !!!!它的工作原理,我會盡量讓它稍後更輕量級。老實說,從來沒有想過有人會幫助我,因爲我的問題有點像[如何重命名現有文件?](http://stackoverflow.com/questions/14970110/how-to-rename-an-existing-file) –

相關問題