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.
Constructor:
- DataInputStream(InputStream in) : Creates a DataInputStream that uses the specified underlying InputStream.
Important Methods:
- 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
Program 1:
//Java program to demonstrate DataInputStream // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class DataInputStreamDemo { public static void main(String args[]) throws IOException { //writing the data. try ( DataOutputStream dout = new DataOutputStream(new FileOutputStream("file.dat")) ) { dout.writeDouble(1.1); dout.writeInt(55); dout.writeBoolean(true); dout.writeChar('4'); } catch(FileNotFoundException ex) { System.out.println("Cannot Open the Output File"); return; } // reading the data back. try ( DataInputStream din = new DataInputStream(new FileInputStream("file.dat")) ) { //illustrating readDouble() method double a = din.readDouble(); //illustrating readInt() method int b = din.readInt(); //illustrating readBoolean() method boolean c = din.readBoolean(); //illustrating readChar() method char d=din.readChar(); System.out.println("Values: " + a + " " + b + " " + c+" " + d); } catch(FileNotFoundException e) { System.out.println("Cannot Open the Input File"); return; } } } |
Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.
Output:
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 contribute@geeksforgeeks.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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Using predefined class name as Class or Variable name in Java
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1

