Programming Tips - Java: Read and parse a CSV file

Date: 2019dec10 Update: 2025oct6 Language: Java Q. Java: Read and parse a CSV file A. line.split(",") doesn't work since you can have commas inside quotes. Shown as a full example:
import java.util.ArrayList; class Demo { public static class Csv { // Handles: // - Quoted cells // - Backslashed chars // - Regular cells (of course) public static ArrayList<String> parseLine(final String csvLine) { ArrayList<String> a = new ArrayList<String>(); boolean inQuote = false; boolean inBackslash = false; String cell = ""; for (int i = 0; i < csvLine.length(); i++) { final char c = csvLine.charAt(i); if (inQuote) { if (c == '"') { inQuote = false; continue; } cell += c; } else if (inBackslash) { cell += c; inBackslash = false; } else { switch(c) { case '"': inQuote = true; break; case '\\': inBackslash = true; break; case ',': a.add(cell); cell = ""; break; default: cell += c; } } } if (!cell.isEmpty()) { a.add(cell); } return a; } } public static final void main(String []args) { { System.out.println("Test 1: Simple..."); final String line = "one,two,three"; var a = Csv.parseLine(line); System.out.println("line=" + line + " ==> " + a); System.out.println(); } { System.out.println("Test 2: Backslash..."); final String bs = "\\"; final String line = "one," + bs + "two,three"; var a = Csv.parseLine(line); System.out.println("line=" + line + " ==> " + a); System.out.println(); } { System.out.println("Test 3: Field with comma..."); final String q = "\""; final String line = "field1," + q + "field, 2" + q + ",field3"; System.out.println("line=" + line); var a = Csv.parseLine(line); for (int i = 0; i < a.size(); i++) { System.out.println("a[" + i + "]=" + a.get(i)); } System.out.println(); } } }
Output:
Test 1: Simple... line=one,two,three ==> [one, two, three] Test 2: Backslash... line=one,\two,three ==> [one, two, three] Test 3: Field with comma... line=field1,"field, 2",field3 a[0]=field1 a[1]=field, 2 a[2]=field3
This is very straight forward. Not especially clever. But it works for me.