I Am Not Getting the Result I Expect Using readLine() in Java

允我心安 提交于 2020-01-29 08:43:07

问题


I am using the code snippet below, however it's not working quite as I understand it should.

public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String line;
    try {
        line = br.readLine();
        while(line != null) {
            System.out.println(line);
            line = br.readLine();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From reading the Javadoc about readLine() it says:

Reads a line of text. A line is considered to be terminated by any one of a line feed (\n), a carriage return (\r), or a carriage return followed immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Throws: IOException - If an I/O error occurs

From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like \r. However, this code just ends up looping infinitely. After debugging, I have found that instead of null being returned when just a termination character is entered, it actually returns an empty string (""). This doesn't make sense to me. What am I not understanding correctly?


回答1:


From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like '\r'.

That is not correct. readLine will return null if the end of the stream is reached. That is, for example, if you are reading a file, and the file ends, or if you're reading from a socket and the socket closses.

But if you're simply reading the console input, hitting the return key on your keyboard does not constitute an end of stream. It's simply a character that is returned (\n or \r\n depending on your OS).

So, if you want to break on both the empty string and the end of line, you should do:

while (line != null && !line.equals(""))

Also, your current program should work as expected if you pipe some file directly into it, like so:

java -cp . Echo < test.txt



回答2:


No input is not the same as the end of the stream. You can usually simulate the end of the stream in a console by pressing Ctrl+D (AFAIK some systems use Ctrl+Z instead). But I guess this is not what you want so better test for empty strings additionally to null strings.




回答3:


There's a nice apache commons lang library which has a good api for common :) actions. You could use statically import StringUtils and use its method isNotEmpty(String ) to get:

while(isNotEmpty(line)) {
    System.out.println(line);
    line = br.readLine();
}

It might be useful someday:) There are also other useful classes in this lib.



来源:https://stackoverflow.com/questions/25033/i-am-not-getting-the-result-i-expect-using-readline-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!