Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does BufferedReader.ready() method ensure that readLine() method does not return NULL?

Tags:

java

file-io

I have such code to read a text file using BufferedReader:

BufferedReader reader=null;
    try {
        reader = new BufferedReader(new FileReader("file1.txt"));

        while (reader.ready()) {
            final String line = reader.readLine();
            System.out.println("<"+line+">");
        } catch (..)
    {
        ...
    }

It works correctly but Findbugs reports a warning:

NP_DEREFERENCE_OF_READLINE_VALUE : The result of invoking readLine() is dereferenced without checking to see if the result is null. If there are no more lines of text to read, readLine() will return null and dereferencing that will generate a null pointer exception.

When I change FileReader to StringReader, i.e.

BufferedReader reader=null;
    try {
        reader = new BufferedReader(new StringReader("ABCD"));

        while (reader.ready()) {
            final String line = reader.readLine();
            System.out.println("<"+line+">");
        } catch (..)
    {
        ...
    }

the readLine method returns null while the ready method always returns true - indeed this is an infinite loop.

This seems that the readLine may return null even if ready returns true. But why does the behavior differ for different Readers?

UPDATE:

I do know the normal way to read a text file (just like Peter and Ali illustrated). but I read that piece of code from my colleague and realized that I don't know the ready method. Then I read the JavaDoc, but don't understand block. Then I did a test and posted this question. So, the better way to put this question might be:

When will the input be blocking? How to use the ready method (or why not to use it)? Why do those 2 Readers (FileReader and StringReader) behave differently with regards to the ready method?

like image 583
chance Avatar asked Mar 09 '11 11:03

chance


People also ask

What does BufferedReader ready do?

The ready() method of BufferedReader class in Java is used to verify whether the buffer stream is ready to be read or not. A buffer stream is said to be ready in two cases either the buffer is not empty or the main stream is ready.

What does BufferedReader readLine return?

BufferedReader readLine() method in Java with Examples Return value: This method returns the String that is read by this method and excludes any termination symbol available. If the buffered stream has ended and there is no line to be read then this method returns NULL.

What is the function of read () and readLine () method of the BufferedReader class?

This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.

Can readLine return null?

BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.


2 Answers

The ready method tells us if the Stream is ready to be read.

Imagine your stream is reading data from a network socket. In this case, the stream may not have ended, because the socket has not been closed, yet it may not be ready for the next chunk of data, because the other end of the socket has not pushed any more data.

In the above scenario, we cannot read any more data until the remote end pushes it, so we have to wait for the data to become available, or for the socket to be closed. The ready() method tells us when the data is available.

like image 86
Parag Avatar answered Oct 01 '22 06:10

Parag


The Reader.ready() and InputStream.available() rarely work as you might like, and I don't suggest you use them. To read a file you should use

String line;
while ((line = reader.readLine()) != null)
    System.out.println("<"+line+">");
like image 25
Peter Lawrey Avatar answered Oct 01 '22 08:10

Peter Lawrey