I'm using Apache Commons CSV to read a CSV file. The file have an information about the file itself (date and time of generation) at the last line.
|XXXX |XXXXX|XXXXX|XXXX|
|XXXX |XXXXX|XXXXX|XXXX|
|File generation: 21/01/2019 17.34.00| | | |
So while parsing the file, I'm getting this as a record(obviously). I'm wondering is there any way to get rid of it from parsing and does Apache Commons CSV have any provision to handle it.
Apache Commons CSV provides a function to ignore the header ( https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#withSkipHeaderRecord-- ), but don't offer a solution to ignore the footer
. But you can simply get all records, except the last one by manually ignoring the last record.
It's a while loop and you wouldn't know when you get to the end until you get to the end. You have two options:
break
the loop when you reach (counter-1) line. line.trim().spit("|").length() > 1
or in your case do some work as long as the number of records per line is greater than 1. This will ensure you don't apply your logic on the lines with just one column which happens to be your last row aka footer. Example taken from Apache commons and modified a litte
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
for (CSVRecord record : records) {
//all lines except the last will result greater than 1
if (record.size() > 1){
//do your work here
String columnOne = record.get(0);
String columnTwo = record.get(1);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.