title: Java replace line in a text file
body: <p>I found this code from another question</p>
  
  <pre><code>private void updateLine(String toUpdate, String updated) throws IOException {
      BufferedReader file = new BufferedReader(new FileReader(data));
      String line;
      String input = "";
  
      while ((line = file.readLine()) != null)
          input += line + "\n";
  
      input = input.replace(toUpdate, updated);
  
      FileOutputStream os = new FileOutputStream(data);
      os.write(input.getBytes());
  
      file.close();
      os.close();
  }
  </code></pre>
  
  <p>This is my file before I replace some lines</p>
  
  <pre><code>example1
  example2
  example3
  </code></pre>
  
  <p>But when I replace a line, the file now looks like this</p>
  
  <pre><code>example1example2example3
  </code></pre>
  
  <p>Which makes it impossible to read the file when there are a lot of lines in it.</p>
  
  <p>How would I go about editing the code above to make my file look what it looked like at the start?</p>