Reading lines with BufferedReader and checking for

2019-01-24 08:50发布

If I have something like this in my code:

String line = r.readLine();  //Where r is a bufferedReader

How can I avoid a crash if the next line is the end of the file? (i.e. null)

I need to read the next line because there may be something there that I need to deal with but if there isn't the code just crashes.

If there is something there then all is OK, but I can't be guaranteed that there will be something there.

So if I do something like: (pseudo code):

if (r.readLine is null)
//End code

else {check line again and excecute code depending on what the next line is}

The issue I have with something like this is, that when I check the line against null, it already moves onto the next line, so how can I check it again?

I've not worked out a way to do this - any suggestions would be a great help.

6条回答
太酷不给撩
2楼-- · 2019-01-24 09:10

In your case you can read the next line because there may be something there.If there isn't anything, your code won't crash.

String line = r.readLine();
while(line!=null){
   System.out.println(line);
   line = r.readLine();
}
查看更多
Luminary・发光体
3楼-- · 2019-01-24 09:22

If you want loop through all lines use that:

while((line=br.readLine())!=null){
    System.out.println(line);
}
br.close();
查看更多
孤傲高冷的网名
4楼-- · 2019-01-24 09:26

A question in the first place, why don't you use "Functional Programming Approach"? Anyways, A new method lines() has been added since Java 1.8, it lets BufferedReader returns content as Stream. It gets all the lines from the file as a stream, then you can sort the string based on your logic and then collect the same in a list/set and write to the output file. If you use the same approach, there is no need to get worried about NullPointerException. Below is the code snippet for the same:-

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class LineOperation {
    public static void main(String[] args) throws IOException {
            Files.newBufferedReader(Paths.get("C://xyz.txt")).
            lines().
            collect(Collectors.toSet()). // You can also use list or any other Collection
            forEach(System.out::println);
    }

}
查看更多
来,给爷笑一个
5楼-- · 2019-01-24 09:27

You can use the following to check for the end of file.

public bool isEOF(BufferedReader br)  
{
     boolean result;

     try 
     {
         result = br.ready();
     } 
     catch (IOException e)
     {
         System.err.println(e);
     }
     return result;
}
查看更多
Emotional °昔
6楼-- · 2019-01-24 09:36

You could purposely have it throw the error inside your loop. i.e.:

String s = "";
while (true) {
    try {
        s = r.readline();
    }catch(NullPointerException e) {
        r.close();
        break;
    }
    //Do stuff with line
}

what everyone else has sad should also work.

查看更多
淡お忘
7楼-- · 2019-01-24 09:37

Am... You can simply use such construction:

String line;

while ((line = r.readLine()) != null) {
   // do your stuff...
}
查看更多
登录 后发表回答