在什么条件下Java的Scanner.hasNextLine()会阻塞? [英] Under what conditions does Java's Scanner.hasNextLine() block?

查看:1013
本文介绍了在什么条件下Java的Scanner.hasNextLine()会阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的javadoc Scanner.hasNextLine()状态:


返回true如果此扫描仪的输入中有另一行。
此方法 可能 在等待输入时阻止。扫描仪不会超过任何输入

Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

该方法会在什么条件下阻止?

Under what conditions will the method block?

推荐答案

这取决于扫描仪从中获取输入的来源。

It depends on the source that the Scanner gets the input from.

例如,如果它是一个文件,整个输入都可用,所以 hasNextLine()不会阻止(因为它可以肯定地知道何时到达文件的末尾并且没有更多的输入。

For example, if it's a file, the entire input is available, so hasNextLine() wouldn't block (since it can know with certainty when the end of the file is reached and there's no more input.

另一方面,如果源是标准输入,那么可以总是更多输入 - 用户总是可以输入更多输入 - 所以 hasNextLine()会阻塞,直到用户键入新的输入行。

On the other hand, if the source is standard input, there can always be more input - the user can always type more input - so hasNextLine() would block until the user types in a new line of input.

这篇关于在什么条件下Java的Scanner.hasNextLine()会阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆