2012-04-22 23 views
3

我正在研究一個項目,該項目有一個讀取文件的方法,找到一個傳入的字符串寫入一個名爲temp的新文件(它沒有傳入行在其中),然後刪除原始並將temp更名爲opriginal名稱。我可以運行它沒有錯誤,但它沒有輸出到新文件。我已經完成了通常的調試,發現錯誤在於它將行寫入新文件的行。我覺得我犯了一個錯誤,提出了一些錯誤。任何解決這將是很大的幫助......Java讀取文件並刪除行,無法找出錯誤

下面是代碼

public static void LineDelete(String Filename, String Content) throws IOException { 
    try { 
     File flights = new File("AppData/" + Filename); 
     File temp; 
     temp = new File("AppData/temp.txt"); 
     FileWriter fstream; 
     BufferedWriter out; 
     try (Scanner sc = new Scanner(flights)) { 

      fstream = new FileWriter("AppData/temp.txt", true); 
      out = new BufferedWriter(fstream); 
      boolean exis = temp.exists(); 
      if (exis) { 
       temp.delete(); 
       temp = new File("AppData/temp.txt"); 
       boolean createNewFile = temp.createNewFile(); 
      } else { 
       boolean creatNewFile = temp.createNewFile(); 
      } 
      String f; 
      while (sc.hasNextLine()) { 
       f = sc.nextLine(); 
       if (!f.equals(Content)) { 

        out.newLine(); 
        out.write(f); 

       } 

      } 
     } 
     fstream.close(); 
     //out.close(); 
     flights.delete(); 
     File flightsn = new File("AppData/" + Filename); 
     temp.renameTo(flightsn); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

}

回答

4

你應該叫收盤out(BufferedReader類)。

您還應該在try-catch-finally的finally子句中關閉它。

你的代碼應該或多或少

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class FileWrite { 
    public static void LineDelete(String Filename, String Content) 
      throws IOException { 
     BufferedWriter out = null; 
     File flights = new File("AppData/" + Filename); 
     File temp = new File("AppData/temp.txt"); 
     FileWriter fstream = null; 

     try { 
      Scanner sc = new Scanner(flights); 
      fstream = new FileWriter("AppData/temp.txt", true); 
      out = new BufferedWriter(fstream); 
      boolean exis = temp.exists(); 
      if (exis) { 
       temp.delete(); 
       temp = new File("AppData/temp.txt"); 
       boolean createNewFile = temp.createNewFile(); 
      } else { 
       boolean creatNewFile = temp.createNewFile(); 
      } 
      String f; 
      while (sc.hasNextLine()) { 
       f = sc.nextLine(); 
       if (!f.equals(Content)) { 
        out.newLine(); 
        out.write(f); 
       } 

      } 
     } catch (IOException ex) { 
      Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      try { 
       out.close(); 
      } catch (Exception e) { 
       Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, e); 
      } 
     } 

     if(flights.exists()){   
      flights.delete(); 
      File flightsn = new File("AppData/" + Filename); 
      temp.renameTo(flightsn); 
     } 
    } 
} 
+0

確定我失去了別的東西,因爲這只是刪除了文件flights.txt ... – Charlie 2012-04-22 18:14:58

+0

好吧,我弄清楚了一點問題,另外一個問題,當它刪除它留下空行,有沒有辦法刪除所有空行... – Charlie 2012-04-22 18:26:45

+0

哎呀,你在這裏關閉前刪除和重命名! – 2012-04-22 19:09:09

相關問題