Bharata
Bharata

Reputation: 737

Detecting empty line while reading file with Java

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.

Upvotes: 0

Views: 13717

Answers (3)

anu
anu

Reputation: 1007

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);
      }    
   }
}

Upvotes: 0

Francesc Lordan
Francesc Lordan

Reputation: 529

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();

Upvotes: 2

nhouser9
nhouser9

Reputation: 6780

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.
}

Upvotes: 1

Related Questions