title: replace a line in a text file by using java
body: <p>How do you replace a line in a text file?</p>
  
  <p>For example you have 1.@@@
  and want to replace it with 1.###</p>
  
  <p>I have program this prgram at the moment.
  You search through a list and if you find a string, that you want. You write the string to another file. My porblem is that I don't know how to replace a line in an existing text file.</p>
  
  <pre><code>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&lt;2){
              tokens[3]="###";
  
              String a1 = Arrays.toString(tokens);
              String a2 = a1.replaceAll("  ", "");
              String a3 = a2.replaceAll(" ", "");
              String a6 = a3.replaceAll(",", ";");
              String a7 = a6.replaceAll("[&lt;&gt;\\[\\],-]", "");
              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){}
  
  }        
  </code></pre>
  
  <p>and this is the list.</p>
  
  <p>0; 000;0;<em>*</em>;0;0;0;</p>
  
  <p>1; 001;1;<em>*</em>;0;0;1;</p>
  
  <p>2; 010;1;<em>*</em>;0;1;0;</p>
  
  <p>3; 011;2;<em>*</em>;0;1;1;</p>
  
  <p>4; 100;1;<em>*</em>;1;0;0;</p>
  
  <p>5; 101;2;<em>*</em>;1;0;1;</p>
  
  <p>6; 110;2;<em>*</em>;1;1;0;</p>
  
  <p>7; 111;3;<em>*</em>;1;1;1;</p>