DataInputStream不推荐使用readLine()方法 [英] DataInputStream deprecated readLine() method
问题描述
我在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()
.
更多替代方案:
- Use a
Scanner
built roundSystem.in
, and callScanner.nextLine
- Use a
Console
(obtained fromSystem.console()
) and callConsole.readLine
这篇关于DataInputStream不推荐使用readLine()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!