java bufferedReader.readline 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17415876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bufferedReader.readline is returning null
提问by Canvas
just a really quick question i have a file in AFC/save.txt which has this in it
只是一个非常简单的问题,我在 AFC/save.txt 中有一个文件,里面有这个
peter
彼得
now I use this code in Java and it returns null, any idea why?
现在我在 Java 中使用这段代码,它返回 null,知道为什么吗?
//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.
该值为空。
回答by frogmanx
Which value is null?
哪个值为空?
At if(br.readLine() != null)
you are reading in the first line of the file.
在if(br.readLine() != null)
您正在阅读的文件的第一行。
At Log.d("File", "Value : " + br.readLine() );
you are at the second line of the file.
在Log.d("File", "Value : " + br.readLine() );
你在文件的第二行。
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.
在 player = br.readLine();
您阅读的文件中的第三行。如果文件中只有一行,该行将返回 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();
}