Avoid blank lines when reading a .csv with opencsv in Java Android

1

I use the opencsv library to read the contents of a .csv file

I have the following:

String fileName = "file:///storage/sdcard1/maptest/cvs_test1.cvs";
final File file = new File(Uri.parse(fileName).getPath());

try {
    CSVReader reader = new CSVReader(new FileReader(file), ',', '"', 1);

    //Read CSV line by line and use the string array as you want
    String[] nextLine;
    try {
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine != null) {
                //Verifying the read data here
                System.out.println(Arrays.toString(nextLine));

            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

I try to detect if a line is blank and thus jump to the next one

    
asked by Webserveis 18.11.2017 в 17:50
source

2 answers

1

I've solved it with:

while ((nextLine = reader.readNext()) != null) {    
    if (nextLine.length == 1 && nextLine[0].isEmpty()) {
        Log.w(TAG, "Read csv: Skip Line Blank");
        continue;
    }
    ...
}
    
answered by 30.11.2017 / 23:36
source
1

Not only that it is different from null but also that it is not empty and that it is not equal to the line break

if (nextLine != null && !nextLine.isEmpty() && !nextLine.equals("\n")) {
                //Verifying the read data here
                System.out.println(Arrays.toString(nextLine));

            }
    
answered by 18.11.2017 в 19:06