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.
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.
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.
readLine only returns null once you reach the end of the file.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With