2016-03-20 93 views
0
try { 

     File inFile = new File("books.txt"); 

     if (!inFile.isFile()) { 
     System.out.println("Parameter is not an existing file"); 
     return; 
     } 

     //Construct the new file that will later be renamed to the original filename. 
     File tempFile = new File(inFile + "temp.txt"); 

     BufferedReader br = new BufferedReader(new FileReader("books.txt")); 
     PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); 

     String line = null; 

     //Read from the original file and write to the new 
     //unless content matches data to be removed. 
     while ((line = br.readLine()) != null) { 

     String lineToRemove; 
     lineToRemove = JOptionPane.showInputDialog("Enter line to remove"); 
     if (!line.trim().contains(lineToRemove)) { 

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

     //Delete the original file 
     if (!inFile.delete()) { 
     System.out.println("Could not delete file"); 
     return; 
     } 

     //Rename the new file to the filename the original file had. 
     if (!tempFile.renameTo(inFile)) 
     System.out.println("Could not rename file"); 

    } 
    catch (FileNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 

我想創建一個方法來刪除我的.txt文件中的一行代碼,如果我輸入該行中包含的單詞。我的問題是,當我運行這個時,測試說它不能刪除該文件。代碼有問題嗎?我的代碼也用#分隔,如果這可能會增加問題。樣本輸出是: 巨人#詹姆斯#1993年JAVA從關鍵字的.txt文件中刪除一行

+2

不要使用'File.delete',使用['Files.delete']](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#delete-java.nio。文件路徑-)。這會引發一個有用的例外來指出失敗的原因;而不是返回一個'boolean'而沒有指出原因。 –

+0

inFile.delete()返回false,表示books.txt不存在。 –

+0

@KaustubhKhare非常不真實。例如,這可能是一個權限問題。它可以存在但不是文件。這可能是其他一些事情。 –

回答

1

我已經測試了細微的變化你的代碼(由@Andreas而我寫這篇說明)和它的作品如預期

public static void main(String[] args) { 
    try { 

      File inFile = new File("C:\\rirubio\\books.txt"); 

      if (!inFile.isFile()) { 
      System.out.println("Parameter is not an existing file"); 
      return; 
      } 

      //Construct the new file that will later be renamed to the original filename. 
      File tempFile = new File("C:\\rirubio\\books.txt" + "_temp"); 

      BufferedReader br = new BufferedReader(new FileReader(inFile)); 
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); 

      String line = null; 

      String lineToRemove = JOptionPane.showInputDialog("Enter line to remove"); 

      //Read from the original file and write to the new 
      //unless content matches data to be removed. 
      while ((line = br.readLine()) != null) { 
      if (!line.trim().contains(lineToRemove)) { 
       pw.println(line); 
       pw.flush(); 
      } 
      } 

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

      //Delete the original file 
      if (!inFile.delete()) { 
      System.out.println("Could not delete file"); 
      return; 
      } 

      //Rename the new file to the filename the original file had. 
      if (!tempFile.renameTo(inFile)) { 
      System.out.println("Could not rename file"); 
      } 

     } catch (FileNotFoundException ex) { 
      ex.printStackTrace(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     } 
+0

謝謝,這工作。我不知道它是否在我的代碼中,但我嘗試了代碼,並刪除了該行和該文件。 – jkjk