Java.io.DataInputStream class in Java | Set 1
A data input stream enables an application to read primitive Java data types from an underlying input stream in a machine-independent way(instead of raw bytes). That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes.
An application uses a data output stream to write data that can later be read by a data input stream. Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8. DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.
Firstmost let us do discuss the constructor of this class
|DataInputStream(InputStream in)||Creates a DataInputStream that uses the specified underlying InputStream.|
Now let us discuss methods of this class that are depicted below in a tabular format as shown below as follows:
|read(byte b)||Reads some number of bytes from the contained input stream and stores them into the buffer array b.|
|read(byte b, int off, int len)||Reads up to length bytes of data from the contained input stream into an array of bytes.|
|readBoolean()||Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.|
|readChar()||Reads two input bytes and returns a char value.|
|readUTF()||Reads data from the underlying input stream and converts the bytes into a Unicode string.|
|readByte()||Read one input byte and returns a byte value.|
|readFloat()||Read four input bytes and returns a float value.|
|readFully()||Read bytes equal to the length of the byte array|
|readDouble()||Reads eight input bytes and returns a double value.|
|readInt()||Reads four input bytes and returns an int value.|
|readLine()||Reading lines of text|
|readLong()||Reading eight input bytes and returns a long value|
|readShort()||Read two input bytes and return a short value.|
|readUnsignedByte()||Read byte and return as an integer|
|readUnsignedShort()||Read two input bytes and returns as an integer array|
|skipBytes()||Skips over n bytes of data from input stream|
Remember: The DataInputStream class is often used together with a DataOutputStream.
Now let us do implement a few of the above methods of this class has been discussed above
Below program uses try-with-resources. It requires JDK 7 or later as concept of try-catch block was introduced in Java7
Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.
Next Article: Java.io.DataInputStream class in Java | Set 2
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.