DataInputStream deprecated readLine() method
Asked Answered
L

4

35

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)
{
}

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

Locular answered 10/4, 2011 at 11:17 Comment(2)
Please include a compilable test case (or a failing test case with the applicable error) next time.Overtone
Test case? We're talking deprecation. You put the code above into an editor and it turns the line yellow and say "readLine is deprecated". No one needs a test case to be told a java method is deprecated. Here's the applicable warning: docs.oracle.com/javase/7/docs/api/java/io/…Nasa
B
38

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().

More alternatives:

  • Use a Scanner built round System.in, and call Scanner.nextLine
  • Use a Console (obtained from System.console()) and call Console.readLine
Bessel answered 10/4, 2011 at 11:20 Comment(0)
T
30

Deprecation and the alternatives is usually already explicitly explained in the javadocs. So it would be the first place to look for the answer. For DataInputStream you can find it here. The readLine() method is here. Here's an extract of relevance:

Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:

    DataInputStream d = new DataInputStream(in);

with:

    BufferedReader d
         = new BufferedReader(new InputStreamReader(in));

The character encoding can then be explicitly specified in the constructor of InputStreamReader.

The Scanner which was introduced since Java 1.5 is also a good (and modern) alternative.

Turncoat answered 10/4, 2011 at 11:27 Comment(0)
C
0

The below doesn't work,

num = Integer.parseInt(in);

Instead you should use:

num = Integer.parseInt(in.readLine());

readLine() will read an input of line until line break.

Capp answered 17/8, 2014 at 14:56 Comment(0)
S
0

calling readLine command in scala returns "warning: there was one deprecation warning; re-run with -deprecation for details" message.

You can handle this warning as illustrated below

val hadoopConfig = spark.sparkContext.hadoopConfiguration
val hdfs = org.apache.hadoop.fs.FileSystem.get(hadoopConfig)
val destinationFile = new org.apache.hadoop.fs.Path(s"hdfs://...")
val outFile = hdfs.open(destinationFile)

val wholeStream = Array.fill[Byte](outFile.available)(0)
outFile.readFully(wholeStream,0,outFile.available)
return new String(wholeStream)
Strobile answered 16/1, 2020 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.