Good day everyone! My target is to make csv reader to skip the blank lines while parsing a file, do nothing basically, only get me the rows with at least one value. At the moment I have two methods -> 1st is just reading all rows as List of Strings array and returns it, 2nd converts the result into List of Lists of Strings, both are bellow:
private List<String[]> readCSVFile(File filename) throws IOException {
CSVReader reader = new CSVReader(new FileReader(filename));
List<String[]> allRows = reader.readAll();
return allRows;
}
public List<List<String>> readFile(File filename) throws IOException {
List<String[]> allRows = readCSVFile(filename);
List<List<String>> allRowsAsLists = new ArrayList<List<String>>();
for (String[] rowItemsArray : allRows) {
List<String> rowItems = new ArrayList<String>();
rowItems.addAll(Arrays.asList(rowItemsArray));
allRowsAsLists.add(rowItems);
}
return allRowsAsLists;
}
My first thought was to check (in the 2'nd method) the length of an array if its 0 just to ignore it - which would be something like this:
for (String[] rowItemsArray : allRows) {
**if(rowItemArray.length == 0) continue;**
List<String> rowItems = new ArrayList<String>();
rowItems.addAll(Arrays.asList(rowItemsArray));
allRowsAsLists.add(rowItems);
}
Unfortunately that didn't work for the reason that even if the row is blank it still returns an array of elements - empty Strings in fact. Checking an individual String is not an option as there are 100+ columns and this is variable. Please suggest what’s the best way to achieve this. Thanks.
Sorted it out this way:
public List<List<String>> readFile(File filename) throws IOException {
List<String[]> allRows = readCSVFile(filename, includeHeaders, trimWhitespacesInFieldValues);
List<List<String>> allRowsAsLists = new ArrayList<List<String>>();
for (String[] rowItemsArray : allRows) {
**if(allValuesInRowAreEmpty(rowItemsArray)) continue;**
List<String> rowItems = new ArrayList<String>();
rowItems.addAll(Arrays.asList(rowItemsArray));
allRowsAsLists.add(rowItems);
}
return allRowsAsLists;
}
private boolean allValuesInRowAreEmpty(String[] row) {
boolean returnValue = true;
for (String s : row) {
if (s.length() != 0) {
returnValue = false;
}
}
return returnValue;
}
readCSVFilemethod.