EoF System.in 处理中
Scanner scanner=new Scanner(System.in);
String s1=scanner.nextLine();
String s2= scanner.nextLine();
我需要在第一个输入处写入 EoF(ctrl D) 。我该如何处理它,以便标准输入不会关闭并且我可以继续接收输入
Scanner scanner=new Scanner(System.in);
String s1=scanner.nextLine();
String s2= scanner.nextLine();
I need to write EoF(ctrl D) at the first input. How can I process it so that stdin doesn't close and I can continue to receive input
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。一旦到达 EOF,输入流就处于无效状态,无法进一步读取。
如果您需要发出类似于 EOF 的信号但允许随后读取,则必须使用程序可识别的一些特殊输入数据。
有一些丑陋的黑客可以阻止 Ctrl+D 被识别为 EOF:
如何在 EOF 后重新打开 System.in 或完全阻止 EOF?。另请参阅https://stackoverflow.com/a/1066647/18157。
You can't. Once EOF is reached, the input stream is in an invalid state for further reading.
If you need to signal something similar to EOF but allow reading subsequently, you must use some special input data that is recognized by the program.
There are ugly hacks to prevent Ctrl+D from being recognized as EOF:
How do I reopen System.in after EOF or prevent EOF entirely?. Also see https://stackoverflow.com/a/1066647/18157.
您可以像最初一样重新打开扫描仪:
请注意,如果流永远标记 EOF,则这是一个无限循环,例如在管道或重定向文件时,因此您有责任验证 stdin 是 tty 和/或添加重试限制。
You can simply reopen the Scanner the same way you did originally:
Note that this is an infinite loop if the stream flags EOF forever, such as when piping or redirecting a file, so it's your responsibility to verify that stdin is a tty and/or add a retry limit.