DataInputStream不推荐使用readLine()方法 [英] DataInputStream deprecated readLine() method

查看:251
本文介绍了DataInputStream不推荐使用readLine()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java 6.使用 DataInputStream in = new DataInputStream(System.in); 来读取用户输入。不推荐使用readLine()时。阅读用户价值的工作原理是什么?

I am on java 6. Using DataInputStream in = new DataInputStream(System.in); to read user input. When the readLine() is deprecated. What is the work around for reading user value?

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()时解释。

please explain as it should when the readLine() is deprecated.

推荐答案

InputStream 基本上是二元构造。如果您想阅读 text 数据(例如,从控制台),您应该使用某些描述的 Reader 。要将 InputStream 转换为 Reader ,请使用 InputStreamReader 。然后在 Reader 周围创建 BufferedReader ,然后使用 BufferedReader.readLine读取一行()

InputStream is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use a Reader of some description. To convert an InputStream into a Reader, use InputStreamReader. Then create a BufferedReader around the Reader, and you can read a line using BufferedReader.readLine().

更多替代方案:


  • 使用 扫描仪 围绕 System.in 构建,并致电 Scanner.nextLine

  • 使用 控制台 (从 System.console()获取)并调用 Console.readLine

  • Use a Scanner built round System.in, and call Scanner.nextLine
  • Use a Console (obtained from System.console()) and call Console.readLine

这篇关于DataInputStream不推荐使用readLine()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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