A data input stream enable an application 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.
- DataInputStream(InputStream in) : Creates a DataInputStream that uses the specified underlying InputStream.
- int read(byte b) : Reads some number of bytes from the contained input stream and stores them into the buffer array b.
Syntax: public final int read(byte b) throws IOException Parameters: b - the buffer into which the data is read. Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. Throws: IOException
- int read(byte b, int off, int len) : Reads up to len bytes of data from the contained input stream into an array of bytes.
Syntax:public final int read(byte b, int off, int len) throws IOException Parameters: b - the buffer into which the data is read. off - the start offset in the destination array b len - the maximum number of bytes read. Returns:the total number of bytes read into the buffer, or -1 if there is no more data Throws: NullPointerException IndexOutOfBoundsException IOException
- boolean readBoolean() : Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
Syntax:public final boolean readBoolean() throws IOException Returns:the boolean value read. Throws: EOFException IOException
- char readChar() : Reads two input bytes and returns a char value.
Syntax:public final char readChar() throws IOException Returns:the next two bytes of this input stream, interpreted as a char. Throws: EOFException IOException
- double readDouble() : Reads eight input bytes and returns a double value.
Syntax:public final double readDouble() throws IOException Returns: the next eight bytes of this input stream, interpreted as a double. Throws: EOFException IOExceptioN
- int readInt() : Reads four input bytes and returns an int value.
Syntax:public final int readInt() throws IOException Returns: the next four bytes of this input stream, interpreted as an int. Throws: EOFException IOException
The DataInputStream class is often used together with a DataOutputStream.
Implementations of some of the important methods
Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.
Values: 1.1 55 true 4
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.