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.
2 Answers
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