0

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.

3
  • Well, I mean your program does exactly what you told it do. When it finds an empty line (line.length() == 0) it exits the loop. Maybe move that condition into the loop body?. Commented Apr 22, 2016 at 1:19
  • 1
    Remove the && length > 0 and 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 do continue Commented Apr 22, 2016 at 1:20
  • Well, The problem here is I can process blank line in the body, but how to check the EOF then? Commented Apr 22, 2016 at 1:35

3 Answers 3

2

Files.readAllLines would load the whole file on memory and that could raise some exceptions. I suggest to parse the file manually with the BufferedReader which allows you to read a whole line at a time.

// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
    if (strLine.isEmpty()){
        // Do whatever you need to do when the empty line is found
    }

//Close the input stream
br.close();
Sign up to request clarification or add additional context in comments.

2 Comments

How can I distinguished between blank line that mark end of file and blank line which has next line?
EOF will be null a blank line will not be null the while loop will only stop when EOF is reached.
1

I suggest you look into the Files.readAllLines() method. It returns a collection of lines. Then you can easily loop through the collection and check each line to see if it is equal to an empty string.

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29

Something like this:

List<String> lines = Files.readAllLines(myfilepath);
for(String line : lines) {
 if (line.trim().isEmpty())
  continue;
 else if ( //etc. you can process each line however you want.
}

5 Comments

Preferably have line.trim().isEmpty()?
is there any benefit of reading all lines beforehand ? that is not really the part of the solution.
@anu doing it this way isn't necessary - as with most things, there are many ways to do it. i happen to think this is the cleanest and best, so this is the answer i gave.
@nhouser9 ah, i see.
Large files could raise OutOfMemoryExceptions
0

Just move the length() check inside the while loop and invert the check, so that you make the program skip the blank lines.

like so:

try{
   FileReader file = new FileReader("file1.txt");
   BufferedReader br = new BufferedReader(file);
   String line;
   while((line=br.readLine()!=null){
      if(line.length() == 0) {
         continue;
      }

      if(checkHeader(line)){
         ProcessHeader(line);
      }else{
         ProcessBody(line);
      }    
   }
}

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.