2016-03-20 17 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ファイル内のコード行を削除するメソッドを作成しようとしています。私の問題は、これを実行すると、ファイルを削除できないというテストがあるということです。コードに何か問題がありますか?私のコードも#で区切られていれば問題に追加されています。サンプル出力は次のようになります。 ジャイアンツ#ジェームス#1993JAVAキーワードを含む.txtファイルから行を削除する

+2

期待どおりに動作しますしないでください'File.delete'を使用し、[' Files.delete'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#delete-java.nio)を使用してください。ファイルパス-)。これは、失敗の原因を示すのに役立つ例外をスローします。その理由を示さないで 'ブール値'を返すのとは対照的に。 –

+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

関連する問題