Skip to content

4 Reading a CSV document

Dan Geabunea edited this page Apr 1, 2015 · 1 revision

Reading a CSV document

The code bellow illustrates how to read a CSV document, with the default reading configuration. All the CSV rows and columns will be imported.

String csvPath = "\home\user\document.csv"
CsvDocument document = CsvDocument.read(csvPath);

Skip the header

The code bellow illustrates how to skip parsing the header while reading a CSV document

String csvPath = "\home\user\document.csv";
CsvConfiguration skipHeaderConfig = new CsvConfiguration(){{
    setSkipHeader(true);
}};
CsvDocument document = CsvDocument.read(csvPath, skipHeaderConfig);

Parse only specific columns

The code bellow illustrates how to read a part of the columns that make up a document. In the example only the first and second column are parsed. The rest of them are ignored.

String csvPath = "\home\user\document.csv";
CsvConfiguration csvConfiguration= new CsvConfiguration(){{
    setColumnIndexesToParse(0, 1);
}};
CsvDocument document = CsvDocument.read(csvPath, csvConfiguration);