How to determine when end of file has been reached?

社会主义新天地 提交于 2019-11-28 21:20:40

You can check using hasNextLine():

Scanner input = new Scanner(new File("\""+filename+"\""));
while(input.hasNextLine())
{
   String data = input.nextLine();
}
Thomas Langston

Line based retrieval may be what you want, but token based can also be useful. You can see in documentation of Scanner

public boolean hasNext()

Returns true if this Scanner has another token in its input. This method may block while waiting for input to scan. The Scanner does not advance past any input.

Specified by: hasNext in interface Iterator<String>

Returns: true if and only if this Scanner has another token

Throws: IllegalStateException - if this Scanner is closed

See Also: Iterator

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