簡體   English   中英

在Java中刪除文件不起作用

[英]delete a file in java does not work

我把這個代碼,這是我從網上找來的,在我的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();
                }
  }
}

我不確定您要刪除的位置,但是在主行的最后一行:

 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行已被注釋掉。

我不確定要如何刪除和重命名文件,因為這對我有用。

我不會試圖弄清楚您的代碼……以及它想做什么。 (您是否聽說過注釋?Javadocs?您考慮過使用它們嗎?)

但是,我想指出, deleterename在多種情況下都可能失敗。 delete情況下,這些內容包括:

  • 目標文件不存在
  • 目標文件確實存在,但是應用程序無權訪問父目錄和/或刪除文件
  • 目標對象是目錄而不是文件
  • (在某些平台上)目標文件被鎖定,因為此應用程序或當前已打開該應用程序。

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


不幸的是,這些方法(在File類上)沒有說明刪除或重命名失敗的原因。 (新的Java 7 Files類上的方法可以執行...)即使它們能夠執行此操作,診斷也將受到OS syscalls報告的限制。 對於Unix / Linux,這是相當有限的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM