title: In Java, how do I overwrite a specific part of a line in a file?
body: <p>I have a <code>csv</code> file thats formatted like this <code>id,text</code>. Here is an example:</p>
  
  <pre><code>helloText,How are you
  goodbyeMessage,Some new text for a change
  errorMessage,Oops something went wrong
  </code></pre>
  
  <p>Now lets say for example I want to edit the text part of <code>goodbyeMessage</code> which is <code>Some new text for change</code>, to <code>See you later</code></p>
  
  <p>The resulting <code>csv</code> should then look like this:</p>
  
  <pre><code>helloText,How are you
  goodbyeMessage,See you later
  errorMessage,Oops something went wrong
  </code></pre>
  
  <p>I have code that can write to the file but when the code finishes executing, this is the resulting <code>csv</code> file:</p>
  
  <pre><code>helloText,How are you
  goodbyeMessage,Some new text for a change
  errorMessage,Oops something went wronggoodbyeMessage,See you later
  </code></pre>
  
  <p>I know this is occurring because I set the FileWriter's <code>append</code> value to <code>true</code>. If I don't everything gets wiped.</p>
  
  <p>I have tried using <code>FileWriter.newLine()</code> to make it look better but that is not what I am trying to achieve. I still want the same number of line in the file.
  MyApp.java</p>
  
  <pre><code>public static void main(String[] args) throws FileNotFoundException, IOException {
      PropsWriter pw = new PropsWriter("test_props.txt");
      pw.updateElementText("goodbyeMessage", "See you later");
  }
  </code></pre>
  
  <p>PropsWriter.java</p>
  
  <pre><code>/**
   * Updates the text of a given element in the properties file.
   * 
   * @param id The id of the element
   * @param newText The text that will replace the original text.
   * 
   * @throws IOException If I/O error occurs
   */
  public void updateElementText(String id, String newText) throws IOException {
      Assertions.checkNotNull(id, "Id must not be null.");
      Assertions.checkNotNull(id, "Id must not be an empty string.");
      File file = new File(pathName);
      BufferedReader br = new BufferedReader(new FileReader(file));
      BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
      try {
          String line;
  
          while((line = br.readLine()) != null) {
              if(line.contains(id)) {
                  //returns true
                  System.out.println("Is line there: " + line.contains(id));
  
                  //returns helloText
                  System.out.println("ID: " + extractId(line));
  
                  //returns How are you
                  System.out.println("TEXT: " + extractText(line));
  
                  //returns Some new text for a change
                  System.out.println("NEW_TEXT: " + newText);
  
                  // This is where I am trying to replace the old text 
                  // with new text that came from the main method.
                  line = line.replaceAll(extractText(line), newText); 
                  //wr.newLine();
                  wr.write(line);             
              }
          }
      } catch(IOException e) {
          e.printStackTrace();
      } finally {
          wr.close();
      }
  }
  
  /**
   * Gets the id part of a line that is stored in the 
   * properties file.
   * 
   * @param element The element the id is got from.
   * @return String representation of the id.
   */
  private static String extractId(String line) {
      final int commaOccurence = getFirstCommaOccurrence(line);
      return line.substring(0, commaOccurence);
  }
  
  /**
   * Gets the text part of a line that is stored in the 
   * properties file.
   * 
   * @param element The element the text is got from.
   * @return String representation of the text.
   */
  private static String extractText(String line) {
      final int commaOccurence = getFirstCommaOccurrence(line);
      return line.substring(commaOccurence + 1, line.length());
  }
  
  /**
   * Gets the first occurrence of a comma in any given line of a text file.
   * @param element
   * @return
   */
  private static int getFirstCommaOccurrence(String line) {
      return line.indexOf(",");
  }
  </code></pre>