Java DataInputStream Class

The DataInputStream class in Java is a subclass of the FilterInputStream class, which provides methods for reading primitive data types from an input stream in a machine-independent way. It implements the DataInput interface, which defines methods for reading primitive data types (byte, short, int, long, float, double, and boolean) as well as strings from an input stream.

Some of the commonly used methods of the DataInputStream class are:

  • readByte(): Reads and returns a byte of data from the input stream.
  • readShort(): Reads and returns a short integer (2 bytes) from the input stream.
  • readInt(): Reads and returns an integer (4 bytes) from the input stream.
  • readLong(): Reads and returns a long integer (8 bytes) from the input stream.
  • readFloat(): Reads and returns a float (4 bytes) from the input stream.
  • readDouble(): Reads and returns a double (8 bytes) from the input stream.
  • readBoolean(): Reads and returns a boolean value (1 byte) from the input stream.
  • readUTF(): Reads and returns a UTF-8 encoded string from the input stream.

It’s important to note that DataInputStream is a blocking I/O operation, meaning that it will wait for data to be available in the input stream before returning. It is also recommended to use a BufferedInputStream along with the DataInputStream to improve performance.

Java DataInputStream class declaration:

The declaration of the DataInputStream class in Java is as follows:

public class DataInputStream extends FilterInputStream implements DataInput {
    // class implementation
}

As you can see, the DataInputStream class is declared as a public class that extends the FilterInputStream class and implements the DataInput interface. By extending the FilterInputStream class, the DataInputStream class inherits all the methods of the FilterInputStream class, which includes the basic I/O operations for reading bytes from an input stream. By implementing the DataInput interface, the DataInputStream class also provides methods for reading primitive data types and strings from an input stream in a machine-independent way.

In addition to the methods inherited from FilterInputStream and DataInput, the DataInputStream class also provides some additional methods such as readFully(byte[] b), which reads a specified number of bytes from the input stream into the given byte array, and skipBytes(int n), which skips over a specified number of bytes in the input stream.

Java DataInputStream class Methods:

The DataInputStream class in Java provides several methods for reading primitive data types and strings from an input stream in a machine-independent way. Here are some of the commonly used methods of the DataInputStream class:

  • readByte(): Reads and returns a byte of data from the input stream.
public byte readByte() throws IOException

readShort(): Reads and returns a short integer (2 bytes) from the input stream.

public short readShort() throws IOException

readInt(): Reads and returns an integer (4 bytes) from the input stream.

public int readInt() throws IOException

readLong(): Reads and returns a long integer (8 bytes) from the input stream.

public long readLong() throws IOException

readFloat(): Reads and returns a float (4 bytes) from the input stream.

public float readFloat() throws IOException

readDouble(): Reads and returns a double (8 bytes) from the input stream.

public double readDouble() throws IOException

readBoolean(): Reads and returns a boolean value (1 byte) from the input stream.

public boolean readBoolean() throws IOException

readUTF(): Reads and returns a UTF-8 encoded string from the input stream.

public String readUTF() throws IOException

readFully(byte[] b): Reads a specified number of bytes from the input stream into the given byte array.

public void readFully(byte[] b) throws IOException

readFully(byte[] b, int off, int len): Reads a specified number of bytes from the input stream into the given byte array starting from the specified offset.

public void readFully(byte[] b, int off, int len) throws IOException

skipBytes(int n): Skips over a specified number of bytes in the input stream.

public int skipBytes(int n) throws IOException

It’s important to note that these methods are blocking I/O operations, meaning that they will wait for data to be available in the input stream before returning. It is also recommended to use a BufferedInputStream along with the DataInputStream to improve performance.

Example of DataInputStream class:

Here’s an example of using the DataInputStream class in Java to read data from a file:

import java.io.*;

public class DataInputStreamExample {
    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new FileInputStream("data.txt");
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);

            // Read and print a byte
            byte b = dataInputStream.readByte();
            System.out.println("Byte read: " + b);

            // Read and print an int
            int i = dataInputStream.readInt();
            System.out.println("Int read: " + i);

            // Read and print a UTF-8 encoded string
            String str = dataInputStream.readUTF();
            System.out.println("String read: " + str);

            dataInputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a FileInputStream to read data from a file named “data.txt”. We then create a DataInputStream to read primitive data types and strings from the FileInputStream. We use the readByte() method to read a byte from the input stream, readInt() method to read an integer, and readUTF() method to read a UTF-8 encoded string from the input stream. Finally, we close both the DataInputStream and FileInputStream. Note that the IOException needs to be caught or declared by the calling method.