title: Why does introducing a FileWriter delete all the content in the file?
body: <p>I have a text file with some text in it and i'm planning on replacing certain characters in the text file. So for this i have to read the file using a buffered reader which wraps a file reader.</p>
  <pre><code>File file = new File(&quot;new.txt&quot;);
  BufferedReader br = new BufferedReader(new FileReader(file));
  
  String line = null;
  while ((line = br.readLine()) != null) {
      System.out.println(line);
  }
  </code></pre>
  <p>But since i have to edit characters i have to introduce a file writer and add the code which has a string method called replace all. so the overall code will look as given below.</p>
  <pre><code>File file = new File(&quot;new.txt&quot;);
  
  FileWriter fw = new FileWriter(file);
  BufferedReader br = new BufferedReader(new FileReader(file));
  
  String line = null;
  while ((line = br.readLine()) != null) {
      System.out.println(line);
      fw.write(br.readLine().replaceAll(&quot;t&quot;, &quot;1&quot;) + &quot;\n&quot;);
  }
  </code></pre>
  <p>Problem is when i introduce a file writer to the code (By just having the initialization part and when i run the program it deletes the content in the file regardless of adding the following line)</p>
  <pre><code>fw.write(br.readLine().replaceAll(&quot;t&quot;, &quot;1&quot;) + &quot;\n&quot;);
  </code></pre>
  <p>Why is this occurring? am i following the correct approach to edit characters in a text file?</p>
  <p>Or is there any other way of doing this?</p>
  <p>Thank you.</p>