Reputation: 5897
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.
Upvotes: 6
Views: 26153
Reputation: 2630
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();
}
Upvotes: 13