The first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.

This:

 while ((newLine = br.readLine()) != null) {
        newLine = br.readLine();
        System.out.println(newLine);
        lines.add(newLine);
    }

Should be changed to this:

 while ((newLine = br.readLine()) != null) {
        System.out.println(newLine);
        lines.add(newLine);
    }

Hence reading a line and processing it, without reading another line and then processing.

Answer from GingerHead on Stack Overflow
🌐
LabEx
labex.io › tutorials › java-reading-a-csv-file-117982
How to Read a CSV File in Java | LabEx
Reading CSV using BufferedReader Data read from CSV file: Row 0: name, age, city Row 1: John, 25, New York Row 2: Alice, 30, Los Angeles Row 3: Bob, 28, Chicago Row 4: Eve, 22, Boston · We import necessary Java classes for file I/O operations and data structures.
🌐
Baeldung
baeldung.com › home › java › java io › reading a csv file into an array
Reading a CSV File into an Array | Baeldung
May 9, 2025 - An advantage of using this approach is being able to use a CSV file with commas embedded within values, along with a comma delimiter: "Kom, Mary",Unbreakable "Isapuari, Kapil",Farishta · Let’s learn how to use this method through an example: List<List<String>> records = new ArrayList<List<String>>(); try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) { String line = ""; while ((line = br.readLine()) != null) { records.add(parseLine(line)); } }
🌐
Blogger
javarevisited.blogspot.com › 2015 › 06 › 2-ways-to-parse-csv-files-in-java-example.html
2 Ways to Parse CSV Files in Java - BufferedReader vs Apache Commons CSV Example
August 7, 2021 - You open a CSV file and start reading it line by line, since each line contains a coma separated String, you need to split them using comma (",") and you will get an array of String containing each column.
🌐
Java67
java67.com › 2015 › 08 › how-to-load-data-from-csv-file-in-java.html
How to load data from CSV file in Java - Example | Java67
You can load data from a CSV file in a Java program by using BufferedReader class from the java.io package. You can read the file line by line and convert each line into an object representing that data. Actually, there are a couple of ways ...
🌐
Stack Abuse
stackabuse.com › reading-and-writing-csvs-in-java
Reading and Writing CSVs in Java
February 20, 2019 - Let's consider the steps to open a basic CSV file and parse the data it contains: ... Create a BufferedReader and read the file line by line until an "End of File" (EOF) character is reached
🌐
javaspring
javaspring.net › blog › bufferedreader-to-read-csv-file-java-example
Efficiently Reading CSV Files in Java with BufferedReader — javaspring.net
BufferedReader is a class in the java.io package. It reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines. It has a buffer of a specified size (by default, it's usually 8192 characters) which reduces the number of actual I/O operations, thus improving performance. CSV files are plain text files where each line represents a row of data, and the values within each row are separated by a delimiter, usually a comma.
Find elsewhere
🌐
Medium
medium.com › tuanhdotnet › methods-and-techniques-for-working-with-csv-files-in-java-4601db73e08f
Methods and Techniques for Working with CSV Files in Java | by Anh Trần Tuấn | tuanhdotnet | Medium
July 19, 2025 - import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class CSVReaderExample { public static void main(String[] args) { String filePath = "data.csv"; // Replace with your CSV file path String line; String delimiter = ","; // Assumes a comma as the delimiter try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { while ((line = br.readLine()) != null) { String[] fields = line.split(delimiter); for (String field : fields) { System.out.print(field…
🌐
Medium
medium.com › @zakariafarih142 › mastering-csv-parsing-in-java-comprehensive-methods-and-best-practices-a3b8d0514edf
Mastering CSV Parsing in Java: Comprehensive Methods and Best Practices | by Zakariafarih | Medium
November 25, 2024 - While this approach reduces dependencies, it requires careful handling of edge cases. Overview: Read the CSV file line by line using BufferedReader and split each line based on the comma delimiter.
🌐
CodeSpeedy
codespeedy.com › home › how to read csv file in java
How to read CSV file in Java - CodeSpeedy
July 2, 2021 - Use the BufferedReader class to read line by line from the input CSV file. Then use a delimiter to split each line into tokens. package PraticeProblems; import java.io.*; public class readCSVUsingSplit { public static void main(String[] args) ...
🌐
Reddit
reddit.com › r/learnjava › reading csv file.
r/learnjava on Reddit: Reading CSV file.
May 29, 2023 -
Ok im trying to read from a csv file and some how my directory can't find it can someone help. The Cvs file is in my project src file which I have named Files as well. 

Heres the code:


 import java.io.BufferedReader;

import java.io.FileReader; import java.io.IOException; import java.io.*; public class Operrations {

public static void main(String[] args) throws Exception {

    String File = "Files\\Crimes.csv";

    BufferedReader reader = null;

    String line = "";

    try {

        reader = new BufferedReader(new FileReader(File));

        while((line = reader.readLine()) !=null);

        String[] row = line.split(",");

        for(String index: row){

            System.out.printf("%-10", index);


        }

        System.out.println();






    




    }


    catch (Exception e){

        e.printStackTrace();



    }


    finally {


        try{


        reader.close();

        } catch(IOException e){

            e.printStackTrace();



        }




    }





}

}

🌐
javaspring
javaspring.net › blog › read-csv-java
Reading CSV Files in Java: A Comprehensive Guide — javaspring.net
In this example, we first open the CSV file using a FileReader and wrap it with a BufferedReader for efficient reading. Then, we read each line of the file using the readLine() method and split the line into an array of values using the split() ...
🌐
Blogger
javarevisited.blogspot.com › 2015 › 06 › 2-ways-to-parse-csv-files-in-java-example.html
Javarevisited: 2 Ways to Parse CSV Files in Java - BufferedReader vs Apache Commons CSV Example
You open a CSV file and start reading it line by line, since each line contains a coma separated String, you need to split them using comma (",") and you will get an array of String containing each column.
🌐
Stack Overflow
stackoverflow.com › questions › 60046021 › writing-to-and-reading-from-a-csv-file-java
bufferedreader - Writing to and Reading From a csv file (Java) - Stack Overflow
February 3, 2020 - public class StudentDataTestExercise { public static void main(String[] args) { File file = new File("./src/data.csv"); File file2 = new File("./src/dataNew.csv"); String line = null; String line2 = null; try (BufferedReader br = new BufferedReader(new FileReader(file)); BufferedWriter bw = new BufferedWriter(new FileWriter(file2)); BufferedReader br2 = new BufferedReader(new FileReader(file2))) { while ((line = br.readLine()) != null) bw.write(line + "\n"); String s = String.format("%d;%s;%s;%s;%s", 123442,"ece", "erkul", "[email protected]","mathe 1, mathe 2, BuK, Anwendungssysteme, Konstruktion 3"); bw.write(s); while ((line2 = br2.readLine()) != null) System.out.println(br2.readLine()); } catch (IOException e) { e.printStackTrace(); } } }
Top answer
1 of 4
1

The following code will read only the first 100 lines of the file and extract the values into a list.

java.nio.file.Path path = java.nio.file.Paths.get(str);
try {
    java.util.List<String> values = java.nio.file.Files.lines(path)
                                                       .limit(100)
                                                       .filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
                                                       .map(line -> {
                                                           String[] words = line.split(" ");
                                                           return words.length == 2 ? words[1] : "";
                                                       })
                                                       .collect(java.util.stream.Collectors.toList());
    System.out.println(values);
}
catch (java.io.IOException xIo) {
    xIo.printStackTrace();
}

According to the sample file in your question, the above code will create the following list.

[JOHN, CA, SF, XYZ, , 25, CATEGORY, ]

If you want a Map instead of a List where the Map key is the value between the double $ characters and the Map value is the part after the space, then

Function<String, String> keyMapper = line -> {
    String[] parts = line.split(" ");
    return parts[0].substring(2, parts[0].length() - 2);
};
Function<String, String> valueMapper = line -> {
    String[] parts = line.split(" ");
    if (parts.length > 1) {
        return parts[1];
    }
    else {
        return "";
    }
};
Path path = Paths.get(str);
try {
    Map<String, String> map = Files.lines(path)
                                   .limit(100)
                                   .filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
                                   .collect(Collectors.toMap(keyMapper, valueMapper));
    System.out.println(map);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}

This will create the following Map

{GROUP=CATEGORY, WEATHER=, CITY=SF, STATE=CA, TIME=, NAME=JOHN, REGION=XYZ, AGE=25}
2 of 4
0

You could use regex here to both detect the name line:

int n = 100; // Max lines
String line;
try (BufferedReader br = new BufferedReader(new FileReader(str))) {
    while ((line = br.readLine()) != null && i++ < n) {
        if (line.matches("\\$\\$NAME\\$\|$.$")) {
            System.out.println(line.split(" ")[1]);
        }
    }
}
🌐
TechVidvan
techvidvan.com › tutorials › read-csv-file-in-java
How to Read CSV file in Java - TechVidvan
July 1, 2020 - Learn how to read CSV file in java in different ways-Using scanner class,Bufferedreader class,Java String.split(), OpenCSV API. Learn how to create CSV file
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-the-data-from-a-csv-file-in-java
How to read data from .csv file in Java?
July 1, 2020 - import java.io.FileReader; import com.opencsv.CSVReader; public class ReadFromCSV { public static void main(String args[]) throws Exception { //Instantiating the CSVReader class CSVReader reader = new CSVReader(new FileReader("D://sample.csv")); //Reading the contents of the csv file StringBuffer buffer = new StringBuffer(); String line[]; while ((line = reader.readNext()) != null) { for(int i = 0; i<line.length; i++) { System.out.print(line[i]+" "); } System.out.println(" "); } } }
🌐
HCL GUVI
studytonight.com › java-examples › reading-a-csv-file-in-java
HCL GUVI | Learn to code in your native language
Take your tech career to the next level with HCL GUVI's online programming courses. Learn in native languages with job placement support. Enroll now!