Sobes.tech
Senior

How to read a large CSV file (1GB+)? How to parse and solve the line break problem at chunk boundaries?

sobes.tech AI

Answer from AI

To read a large CSV file (1GB+) in Java and correctly parse lines that may be broken across chunks, you can use the following approach:

  • Read the file in parts (chunks) using BufferedReader or FileChannel.
  • When reading a chunk, save the remainder of the last incomplete line and add it to the next chunk.
  • Use specialized CSV parsers that support streaming reading (e.g., OpenCSV, Apache Commons CSV).

Example with BufferedReader and OpenCSV:

try (BufferedReader reader = new BufferedReader(new FileReader("large.csv"))) {
    CSVParser parser = new CSVParserBuilder().withSeparator(',').build();
    CSVReader csvReader = new CSVReaderBuilder(reader).withCSVParser(parser).build();
    String[] nextLine;
    while ((nextLine = csvReader.readNext()) != null) {
        // Process the line
    }
}

This approach guarantees that lines will be read completely, even if they are long and may be broken when reading in parts. If you need more low-level control, you can implement buffering and manually concatenate incomplete lines.