Skip to content
Morten Laukvik edited this page Oct 29, 2016 · 1 revision

The easiest way to read a CSV file is to call the default constructor. This method will try to auto detect separator character and encoding.

CSV csv = new CSV(new File("presidents.csv"));

To read files using a SEMI COLON as separator character

CSV csv = new CSV();
csv.readFile( new File("presidents.csv"), CSV.SEMICOLON );

To read files using a semi colon as PIPE character

CSV csv = new CSV();
csv.readFile( new File("presidents.csv"), CSV.PIPE );

To read files using a semi colon as TAB character

CSV csv = new CSV();
csv.readFile( new File("presidents.csv"), CSV.TAB );

Reading file with very large data sets

try (CsvReader r = new CsvReader( new File("presidents"), Charset.forName(charset)) )) {
    while (r.hasNext()) {
        Row row = r.next();
    }
}
catch (IOException e) {
    e.printStacktrace();
}

Reading POJO

List<Employee> employees = new ArrayList<>();
employees.add( new Employee("Bob",25,false) );
employees.add( new Employee("Jane",24,true) );
employees.add( new Employee("Yay",32,false) );
CSV csv = new CSV();
csv.readJava(employees);