So I have a problem with reading a file. My code is simple with using BufferedRead. My text consist of header and body like below:
>Header1
This is body1 line1
This is body1 line2
>Header2
This is body2 line1
This is body2 line2
This is body2 line3
This is body2 line4
So basically, I check each line whether it is header or not and then process it according to the procedure for header and body.
try{
FileReader file = new FileReader("file1.txt");
BufferedReader br = new BufferedReader(file);
String line;
while((line=br.readLine()!=null && line.length()>0){
if(checkHeader(line)){
ProcessHeader(line);
}else{
ProcessBody(line);
}
}
}
The problem is, I realize that because this is a downloadable fie from a server, sometimes the file has blank line randomly. For example, after 10 Header and body, there is blank space. The next blank space maybe after 15 header/body. It will become like below:
>Header1
This is body1 line1
This is body1 line2
>Header2
This is body2 line1
This is body2 line2
This is body2 line3
This is body2 line4
.<not blank line>
.<not blank line>
.<not blank line>
>Header10
Body10 line1
Body10 line2
>Header11
Body11 line1
Body11 line2
.<not blank line>
.<not blank line>
.<not blank line>
>Header 25
Body25 line1
Body25line2
>Header 26
.<not blank line>
.<not blank line>
...
How can I detect such blank line because currently, my while loop will stop after it finds a blank line? How can I check for the end of file? Thank you.
line.length() == 0) it exits the loop. Maybe move that condition into the loop body?.&& length > 0and just replace maybe with an if statement inside? and check for more things there w..e you want it to do and if it is blank though just docontinue