Open In App

Java.io.DataInputStream class in Java | Set 1

Improve
Improve
Like Article
Like
Save
Share
Report

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 

Constructor Action Performed
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: 

Methods  Action performed 
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.

Implementation:

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

Example 1 

Java




// Java program to Demonstrate DataInputStream Class
  
// Importing I/O classes
import java.io.*;
  
// Main class
class DataInputStreamDemo {
  
    // Main driver method
    public static void main(String args[]) throws IOException {
  
        // Writing the data
  
        // Try block to check for exceptions
        try ( DataOutputStream dout =
                        new DataOutputStream(new FileOutputStream("file.dat")) ) {
  
            dout.writeDouble(1.1);
            dout.writeInt(55);
            dout.writeBoolean(true);
            dout.writeChar('4');
        }
  
        // Catch block to handle the exceptions
        catch (FileNotFoundException ex) {
  
            // Display message when FileNotFoundException occurs
            System.out.println("Cannot Open the Output File");
            return;
        }
  
        // Reading the data back.
  
        // Try block to check for exceptions
        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();
  
            // Print the values
            System.out.println("Values: " + a + " " + b + " " + c + " " + d);
        }
  
        // Catch block to handle the exceptions
        catch (FileNotFoundException e) {
  
            // Display message when FileNotFoundException occurs
            System.out.println("Cannot Open the Input File");
            return;
        }
    }
}


Output:

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



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads