为什么不能if (txtLine == null) { break; };
工作?或者正确的答案是为什么它仍然将字符串txtLine
设置为空(字面意思)。我理解它的方式,它应该打破字符串为空的那一刻?我不希望它将字符串设置为“null”。但停止时,有在* .txt文件没有更多的行java:使用Buffered Reader并检查字符串是否为空
try{
BufferedReader txtReader = new BufferedReader (new FileReader ("test.txt"));
while (true) {
// Reads one line.
println(txtLine);
if(txtLine == null){
break;
};
txtLine = txtReader.readLine();
nLines(txtLine);
}
txtReader.close();
} catch (IOException ex) {
throw new ErrorException(ex);
}
的txtFile
变量定义为伊娃
private int nChars = 0;
private String txtLine = new String();
private ArrayList <String> array = new ArrayList <String>();
txtLine在哪里定义?你能否把它添加到你的代码片段? –
如果你通过Java中的'Scanner'或者'BufferedReader'来读取用户输入的内容 - >你会立即得到数以百万计的结果,这将清除你的疑问。 –
nLines做什么? –