0

I am using buffer-reader to read data from a .csv file, some of the rows of the the .csv file are empty, I want to skip those rows and read data from rows which have data.

0

2 Answers 2

1

You can use a regular expression while reading the csv file like this one

BufferedReader reader = new BufferedReader(new FileReader(new File("csv.csv")));

        String currentLine;
        while((currentLine = reader.readLine()) != null){
            if(currentLine.matches("(\\d+)(,\\s*\\d+)*")){
                doWhateverYouWant(currentLine);
            }else{
                System.out.println(String.format("Invalid line: %s", currentLine));
            }
        }

Edit: If you are using JDK8, this approach is simpler

reader.lines().filter(line -> line.matches("(\\d+)(,\\s*\\d+)*"))

For the input (note the blank line between the 2nd row and the 4th)

1,2,3,4
1,2,3,4

1,2,3,4

Shows the following output

1,2,3,4
1,2,3,4
Invalid line: 
1,2,3,4
Sign up to request clarification or add additional context in comments.

Comments

0

To ignore blank lines, add this check to make sure the line has something:

while ((line = reader.readLine()) != null) {
if(line.length() > 0) {
  //do lots of stuff to sort the data into lists etc
}           
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.