Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bufferedReader.readline is returning null

just a really quick question i have a file in AFC/save.txt which has this in it

peter

now I use this code in Java and it returns null, any idea why?

//Android
try {
        InputStream fis = game.getFileIO().readFile("AFC/save.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        if(br.readLine() != null)
        {
                            Log.d("File", "Value : " + br.readLine() );
            player = br.readLine();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the value is null.

like image 330
Canvas Avatar asked Jul 02 '13 01:07

Canvas


People also ask

Why does readLine () return null?

If the Ctrl+Z key combination (followed by Enter on Windows) is pressed when the method is reading input from the console, the method returns null . This enables the user to prevent further keyboard input when the ReadLine method is called in a loop.

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 will br readLine () return when the BufferedReader reaches the end of the file?

readLine only returns null once you reach the end of the file.

What happens if you don't close a BufferedReader?

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. So, if you don't close(), system resources may be still associated with the reader which may cause memory leak.


1 Answers

Which value is null?

At if(br.readLine() != null) you are reading in the first line of the file.

At Log.d("File", "Value : " + br.readLine() ); you are at the second line of the file.

At player = br.readLine(); you are reading the third line of the file. If there is only one line in the file, this line will return null.

Try:

try {
    String temp;
    InputStream fis = game.getFileIO().readFile("AFC/save.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    if((temp = br.readLine()) != null)
    {
          player = temp;
          Log.d("File", "Value : " + player );
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
like image 74
frogmanx Avatar answered Sep 29 '22 02:09

frogmanx