public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new FileReader("Test3.txt"));
String line =null;
while((line=in.readLine())!=null)
{
System.out.println(line);
}
}
想问一下通过readLine方法读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行,但是在最后一行的时候并没有换行或者回车的字符啊,此时为什么会读取到最后一行呢?按理说最后一行是不应该被读取到的。求解!
There is a private method in BufferedReader that is used to determine whether the end of the Stream is reached, as follows:
@return the number of chars read into the buffer, or -1 if the end of the source stream has been reached.
And there is a paragraph in readLine() that says:
You can look at the source code of BufferedReader for this. In fact, there is another situation, that is, when the end of the file is read (that is, the last line), this line will be returned even if there are no n and r.