replaceAll( ) not working in replacing lines in a CSV txt file

Viewed 102

I'm trying to replace a certain line in a text file using the replaceAll() method but every time i try to run it, it doesn't change at all. I even tried to covert the string oldUser to a regex (Pattern.quote(oldUser)), am I missing something?

Text File Contents (person.txt):

ABCDEFG1,THERONE ALMADIN,theronealmadin@gmail.com,DAVAO CITY,06/09/1999,09226217885,8000
GFEDCBA2,NATHAN ANG,nathanang@gmail.com,BUTUAN CITY,01/02/2000,09451236987,8000
ADEACAS3,ANTON MARITIREZ,antonmaritirez@gmail.com,GENSAN CITY,02/13/2001,09214561236,8000
AABBCCDD,NAME HERE,emailhere@gmail.com,CITY HERE CITY,12/31/2000,11112223333,8000

My code in particular:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) throws IOException {
        String oldUser = "AABBCCDD,NEW NAME,emailhere@gmail.com,CITY HERE CITY,12/31/2000,11112223333,8000";

        String newUser = "AABBCCDD,NAME HERE,emailhere@gmail.com,CITY HERE CITY,12/31/2000,11112223333,8000";

        String filepath = "person.txt";
        Scanner scanner = new Scanner (new File(filepath));

        StringBuilder bufferedList = new StringBuilder();

        while (scanner.hasNextLine()){
            bufferedList.append(scanner.nextLine()).append(System.lineSeparator());
        }

        String personContents = bufferedList.toString();
        System.out.println("old data: " + personContents);
        scanner.close();

        System.out.println("REGEX" + Pattern.quote(oldUser));
        personContents = personContents.replaceAll(Pattern.quote(oldUser), newUser);

        FileWriter writer = new FileWriter(filepath);

        System.out.println("new data: " + personContents);
        writer.append(personContents);
        writer.flush();

    }
}
1 Answers

Okay so im gonna answer my own question here.

I had the parameters for replaceAll() switched.

Related