I have a csv file thats formatted like this id,text. Here is an example:
helloText,How are you
goodbyeMessage,Some new text for a change
errorMessage,Oops something went wrong
Now lets say for example I want to edit the text part of goodbyeMessage which is Some new text for change, to See you later
The resulting csv should then look like this:
helloText,How are you
goodbyeMessage,See you later
errorMessage,Oops something went wrong
I have code that can write to the file but when the code finishes executing, this is the resulting csv file:
helloText,How are you
goodbyeMessage,Some new text for a change
errorMessage,Oops something went wronggoodbyeMessage,See you later
I know this is occurring because I set the FileWriter's append value to true. If I don't everything gets wiped.
I have tried using FileWriter.newLine() 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
public static void main(String[] args) throws FileNotFoundException, IOException {
    PropsWriter pw = new PropsWriter("test_props.txt");
    pw.updateElementText("goodbyeMessage", "See you later");
}
PropsWriter.java
/**
 * 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(",");
}
 
     
     
    