2012-01-26 77 views
0

我把這個代碼,這是我從網上找來的,在我的java程序,但是當我嘗試刪除,原來的文件無法刪除臨時文件不能被重命名爲原來的文件。兩個文件保持在文件夾中,其內容不變。刪除Java中的文件不起作用

...

public class FilingDatabase { 
    public static void main(String[]args)throws IOException{ 
     (new FilingDatabase()).run(); 
     FilingDatabase fd=new FilingDatabase(); 
     String word = null; 
     fd.delete("person.txt",word); 

     } 

public void run() throws IOException{ 
    File file=new File("person.txt"); 
    BufferedReader br=new BufferedReader(new FileReader(file)); 

    while((str=br.readLine())!=null) 
     i++; 

     System.out.print("\t\t\t\t\t\t***************WELCOME*****************"); 
     System.out.println(); 
     System.out.println("1. Add \n2. Edit \n3. Delete \n4. Exit"); 
     System.out.print("\nEnter option number: "); 
     option=in.next(); 

     while(true){ 
      ... 
      else if(option.charAt(0)=='3'){ 
       // FilingDatabase fd= new FilingDatabase(); 
       System.out.print("Enter word: "); 
       word=in.next(); 
       //delete("person.txt",word); 

      }   
      ... 
     } 
    }//end of fxn run() 

....

public void delete(String file, String lineToRemove) throws IOException{ 

     try { 
        File inFile = new File(file); 
        if (!inFile.isFile()){ 
        System.out.println("File does not exist"); 
        return; 
         } 
         File tempFile = new File(inFile.getAbsolutePath() + ".tmp"); 
         BufferedReader br = new BufferedReader(new FileReader(file)); 
         //Scanner br=new Scanner(file); 
         PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); 

         String line = null; 

         while ((line = br.readLine()) != null) { 
         if (!line.trim().equals(lineToRemove)) { 

          pw.println(line); 
          pw.flush(); 
         } 
         } 


         pw.close(); 
         br.close(); 

         if (!inFile.delete()) { 
         System.out.println("Could not delete file"); 
         return; 
         } 
         if (!tempFile.renameTo(inFile)) 
         System.out.println("Could not rename file"); 

      }catch (FileNotFoundException e) { 
       e.printStackTrace(); 
       } 
    } 
} 
+0

爲什麼不能用同樣的'FillingDatabase'實例'run'和'delete'(而不是把兩個差情況下)? – Jeffrey

+0

'fd.delete(「person。txt「,word);'爲什麼你在word中傳遞null值 – RanRag

+0

@RanRag word是null,因爲我試圖從用戶的輸入中獲取它,但它仍然不起作用 – claire0us

回答

2

我不知道你在哪裏試圖刪除,但你的主要的最後一行:

fd.delete("person.txt",word); 

不會刪除因爲Object.equals(null)應該總是返回false。 (wordnull。)

如果你想刪除循環內:

// FilingDatabase fd= new FilingDatabase(); 
System.out.print("Enter word: "); 
word=in.next(); 
//delete("person.txt",word); 

因爲delete行註釋掉它不會刪除任何東西。

我不知道該怎麼告訴你有關刪除和重命名文件,因爲這對我的作品。

2

我不會試圖讓我的頭在你的代碼......並且它是什麼要做。 (您是否聽說過評論?Javadocs?您是否考慮過使用它們?)

但是,我想指出deleterename在很多情況下都會失敗。在delete的情況下,這些包括以下內容:

  • 目標文件不存在
  • 目標文件確實存在,但應用程序沒有權限訪問父目錄和/或刪除文件
  • 目標對象是一個目錄不是文件
  • (在某些平臺上)的目標文件被鎖定,因爲該應用程序或另一個目前已經打開。

rename的情況下,你必須要考慮的存在,權限等文件的重命名(和它的父目錄),你正試圖移動目錄。還有一個問題是重命名在不同文件系統之間不起作用。


不幸的是,這些方法(在File類)不說爲什麼刪除或重命名失敗。 (在新的Java 7 Files類中的方法做...)即使他們能夠做到這一點,診斷會由什麼OS系統調用報告的限制。在Unix/Linux的情況下,這是非常有限的。

+0

值得注意的是,文件'方法沒有說明爲什麼他們失敗了,在Java 7中,相應的['Files'](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html)方法('delete'和'move')。 – Jeffrey

+0

@Jeffrey - 好點 –