Search code examples
javaarraysstringsplitline

split method to output values under each other when reading from a file


My code works fine however it prints the values side by side instead of under each other line by line. Like this:

iatadult,DDD,

iatfirst,AAA,BBB,CCC

I have done a diligent search on stackoverflow and none of my solution's seem to work. I know that I have to make the change while the looping is going on. However none of the examples I have seen have worked. Any further understanding or techniques to achieve my goal would be helpful. Whatever I am missing is probably very small. Please help.

String folderPath1 = "C:\\PayrollSync\\client\\client_orginal.txt";
File file = new File (folderPath1);
ArrayList<String> fileContents = new ArrayList<>(); // holds all matching client names in array

try {
    BufferedReader reader = new BufferedReader(new FileReader(file));// reads entire file
    String line;

    while (( line = reader.readLine()) != null) { 
        if(line.contains("fooa")||line.contains("foob")){
            fileContents.add(line);
        }
        //---------------------------------------
    }
    reader.close();// close reader
} catch (Exception e) {
    System.out.println(e.getMessage());
}

System.out.println(fileContents);

Solution

  • Add a Line Feed before you add to fileContents.

    fileContents.add(line+"\n");