Java I/O 的 InputStreamReader 或控制台
在 Java 中获取用户输入哪种方法更好?
reader = new BufferedReader(new InputStreamReader(System.in));
或者 通过使用控制台--> Console c = System.console();
两者有什么区别,该使用哪一个。或者说除了这两种还有其他更好的方法吗?
To get user input in Java which is the better method?
reader = new BufferedReader(new InputStreamReader(System.in));
or
by use of Console--> Console c = System.console();
what is the difference between the two and which one is to be used. Or is there any other better method other than these two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java (Java 6) 最近引入了控制台,以便更轻松地读取和写入命令行。因此,我会使用控制台。
Console was introduced recently to Java (Java 6) to make reading and writing to the command line easier. Therefore, I would use Console.
我建议您 Scanner 类来获取来自控制台的用户输入
和其他漂亮的方法,
但在这两者之间我推荐你控制台
I suggest you Scanner class to get user input from console
and other beautiful methods
but between these two I recommend you Console
它们几乎是相同的,但是,控制台有其他方法可以安全地读取用户的信息(例如密码),并返回更直观的结果来判断底层平台是否确实支持控制台操作或以非交互式方式启动。环境
they are pretty much the same, however, the Console has additional methods for securely reading information from the user (eg. password) and returns a more intuitive result for whether the underlying platform actually does support console operations or is launched in a non-interactive environment
恕我直言,他们完全不同。正如医生所说:
因此,实现是特定于操作系统的,而不是独立于平台的(可能有也可能没有)。
使用Consol,甚至您无法从标准输入读取:
如果测试使用控制台,它将无法工作。因此用户必须从控制台给出输入。
IMHO they are total diferent. As the doc says:
So the implementation is OS specific and not platform independent (there could be or not).
With Consol even you can not read from stdin:
If Test is using Console it won't work. So the user MUST give input from the console.