不推荐使用DataInputStream的readLine()方法

一些Java家伙:

我在Java 6上。DataInputStream in = new DataInputStream(System.in);用于读取用户输入。不推荐使用readLine()的情况。读取用户价值的方法是什么?

DataInputStream in = new DataInputStream(System.in);
int num;
try
{
  num = Integer.parseInt(in.readLine()); //this works

  num = Integer.parseInt(in);  //just in doesnt work.
}
catch(Exception e)
{
}

请在不建议使用readLine()时进行解释。

乔恩·斯基特(Jon Skeet):

InputStream从根本上说是二进制结构。如果要读取文本数据(例如从控制台),则应使用Reader某种描述。要将转换InputStreamReader,请使用InputStreamReader然后在BufferedReader周围创建一个Reader,您可以使用读取一行BufferedReader.readLine()

更多选择:

  • 使用Scanner内置回合System.in,然后致电Scanner.nextLine
  • 使用Console(从中获得System.console())并致电Console.readLine

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章