Keyboard Input DataInputStream

Keyboard Input DataInputStream


In keyboard reading, three programs are given using DataInputStream, BufferedReader and Scanner. Of all, using java.util.Scanner is the easier and includes many methods to check the input data is valid to read.

Example on Keyboard Input DataInputStream

Following program uses readLine() method of DataInputStream to read data from the keyboard.

Keyboard Input DataInputStream
Output screenshot on Keyboard Input DataInputStream

The readLine() method of DataInputStream reads a line at a time and returns as a string, irrespective of what the line contains. Depending on the input value, the string is to be parsed into an int or double etc. This is extra work in the keyboard reading when compared to C/C++. In C/C++, parsing is done implicitly by %d and %f etc.

Note: This program compilation raises deprecation warning (observe the screen shot) as readLine() method of DataInputStream is deprecated, but the program works.

What is "System.in"?

" in" is an object of " InputStream" class defined in System class (like "out" is an object of PrintStream class defined in System class). It is declared as static and final object. The in object is connected implicitly to the " standard input stream" of the underlying OS.

DataInputStream dis = new DataInputStream(System.in);

System.in is a byte stream which is automatically connected to the keyboard reading mechanism of operating system. It is a low-level byte stream. The low-level byte stream is passed to high-level byte stream DataInputStream following the rules of chaining. It is chained to DataInputStream to facilitate the reading of user input with it 's readLine() method.

Like System.in is connected to the keyboard mechanism of OS, the System.out is connected to the standard output mechanism of OS.

11 thoughts on “Keyboard Input DataInputStream

  1. amar Kumar

    Thank you Sir A very good Explanation of Each and Every concept.But i don’t understand what is the relationship between System class and PrintStream class so that variable of System class ‘out’ of type PrintStream call method of PrintStream class means ‘println’.
    Sir, if it is Possible give an example where for access the data we use
    classname.classvarible.otherclassmethod();

    1. S. Nageswara Rao, Corporate Trainer Post author

      System.in is an InputStream connected implicitly to the in stream of OS. in stream of OS carries data from keyborad to CPU. So any data travelling between keyboard and CPU can be captured with System.in. This System.in stream is chained to DataInputStream to read the data from keyboard using the methods of DataInputStream like readLine() etc.

Leave a Reply

Your email address will not be published. Required fields are marked *