title: Java: replace only one line/string in the file
body: <p>I have used the following code to replace <code>text</code> to <code>word</code> (taken from <a href="https://stackoverflow.com/questions/25220340/java-replace-line-in-a-text-file">here</a>):</p>
  
  <pre><code>String targetFile = "filename";
  String toUpdate = "text";
  String updated = "word";
  
  public static void updateLine() {
          BufferedReader file = new BufferedReader(new FileReader(targetFile));
          String line;
          String input = "";
  
          while ((line = file.readLine()) != null)
              input += line + "\n";
  
          input = input.replace(toUpdate, updated);
  
          FileOutputStream os = new FileOutputStream(targetFile);
          os.write(input.getBytes());
  
          file.close();
          os.close();
  }
  </code></pre>
  
  <p>and I have a file where I want replace only second line (<code>text</code>):   </p>
  
  <pre><code>My text
  text
  text from the book
  The best text
  </code></pre>
  
  <p>It's work fine, but it replaced all <code>toUpdate</code> strings in the file. How I can edit the code to replacing only one line/string (which completely like <code>toUpdate</code> string) in the file?</p>
  
  <p>The expected file should look like this:</p>
  
  <pre><code>My text
  word
  text from the book
  The best text
  </code></pre>
  
  <p>Is this possible?</p>