2013-08-29 74 views
-1

如何替換文本文件中的一行?用java替換文本文件中的一行

例如你有1。@@@ 並想用1來取代它。###

我有這個程序在prgram的時刻。 您可以通過列表進行搜索,並且如果找到了所需的字符串。您將該字符串寫入另一個文件。我的問題是我不知道如何替換現有文本文件中的一行。

private static BufferedReader br; 



public static void main(String[] args) throws Exception{ 


    try{ 
    FileInputStream fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\\Foutverbeterende codes\\genereren append testbinair apart.txt"); 
    br = new BufferedReader(new InputStreamReader(fstream)); 
    String strLine; 


    //Read File Line By Line 
    while ((strLine = br.readLine()) != null) { 
     String tokens[] = strLine.split(";"); 
     int x = Integer.parseInt(tokens[2]); 



     if(x<2){ 
      tokens[3]="###"; 

      String a1 = Arrays.toString(tokens); 
      String a2 = a1.replaceAll(" ", ""); 
      String a3 = a2.replaceAll(" ", ""); 
      String a6 = a3.replaceAll(",", ";"); 
      String a7 = a6.replaceAll("[<>\\[\\],-]", ""); 
      String a8 = a7 + ";"; 


      System.out.println(a8); 





      FileWriter fwriter = new FileWriter("d is 2.txt", true); 
      PrintWriter outputFile = new PrintWriter(fwriter); 

      outputFile.println(a8); 
      outputFile.close(); 

     } 



    } 


    } 


    catch(Exception e){} 

}   

這就是這個清單。

0; 000; 0; *; 0; 0; 0;

1; 001; 1; *; 0; 0; 1;

2; 010; 1; *; 0; 1; 0;

3; 011; 2; *; 0; 1; 1;

4; 100; 1; *; 1; 0; 0;

5; 101; 2; *; 1; 0; 1;

6; 110; 2; *; 1; 1; 0;

7; 111; 3; *; 1; 1; 1;

+0

可能重複的[爪哇逐行讀取文件中的行和替換第n列(http://stackoverflow.com/questions/18478820/java-read-file-line-by-line-and-replace-第n列) –

+0

幾天前我回答了一個類似於這個問題。我知道它不是*完全*你在找什麼,但它會給你一個關於如何重寫'File'的總體思路。所有你需要做的是做一些小的修改。參考[this](http://stackoverflow.com/a/18479057/1255746)。隨意問任何你可能有的問題。 –

回答

0
// it's okay to throw Exception in main, but ONLY during testing. NEVER EVER 
// in production code! 
public static void main(String[] args) throws Exception{ 
    FileWriter fwriter = null; 
    FileInputStream fstream = null; 
    try { 
     fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\Foutverbeterende codes\\genereren append testbinair apart.txt"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
     String strLine; 

     // this has to be moved OUT of the loop. 
     fwriter = new FileWriter("d is 2.txt", true); 
     PrintWriter outputFile = new PrintWriter(fwriter); 


     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      String tokens[] = strLine.split(";"); 
      int x = Integer.parseInt(tokens[2]); 

      if(x<2){ 
       tokens[3]="###"; 

       String replaced = Arrays.toString(tokens) 
           .replaceAll(" ", ""); 
           .replaceAll(" ", ""); 
           .replaceAll(",", ";"); 
           .replaceAll("[<>\\[\\],-]", ""); 
       replaced += ";"; 

       System.out.println(replaced); 

       outputFile.println(replaced); 
      } 
     } 

    // finally makes sure, that this block is executed, even if something 
    // goes wrong. 
    } finally { 
     if (fstream != null) 
      fstream.close(); 
     if (fwriter != null) 
      fwriter.close(); 
    } 
}