Implementation
To use the DataInputStream class in your Java program, you need to follow these steps:
1. Import the java.io package:
import java.io.*;
2. Create an instance of the input stream from which you want to read data. This can be any type of InputStream, such as FileInputStream or ByteArrayInputStream.
InputStream inputStream = new FileInputStream("data.bin");
3. Create an instance of DataInputStream by passing the input stream to its constructor.
DataInputStream dataInputStream = new DataInputStream(inputStream);
4. Use the available methods of DataInputStream to read primitive data types or strings from the input stream.
int intValue = dataInputStream.readInt();
double doubleValue = dataInputStream.readDouble();
String stringValue = dataInputStream.readUTF();
5. Close the DataInputStream & the underlying input stream when you are done reading data.
dataInputStream.close();
inputStream.close();
It's important to handle exceptions that may occur while reading from the input stream. You can use a try-catch block to catch any IOException that may be thrown.
try {
// Create input stream & DataInputStream
// Read data using DataInputStream methods
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the streams in the finally block
try {
if (dataInputStream != null)
dataInputStream.close();
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Java DataInputStream Class Declaration
The DataInputStream class is declared as mentioned below:
public class DataInputStream extends FilterInputStream implements DataInput
Let's understand this declaration in little more detail:
1. `public`: The DataInputStream class is declared as public, which means it can be accessed from any package.
2. `class`: It is a class, which is a blueprint for creating objects.
3. `DataInputStream`: The name of the class.
4. `extends FilterInputStream`: DataInputStream is a subclass of FilterInputStream. FilterInputStream is an abstract class that provides a basic framework for implementing input streams that can be filtered or transformed. By extending FilterInputStream, DataInputStream inherits its properties and methods.
5. `implements DataInput`: DataInputStream implements the DataInput interface. The interface defines methods for reading primitive data types and strings from a binary stream. By implementing this interface, DataInputStream provides an implementation for these methods.
The DataInputStream class has several constructors that allow you to create instances of the class:
DataInputStream(InputStream in)
This constructor creates a DataInputStream that reads from the specified input stream.
protected DataInputStream(InputStream in, int size)
This protected constructor creates a DataInputStream with a specified buffer size. It is rarely used directly and is primarily intended for subclasses.
When you create a DataInputStream, you must provide an underlying input stream from which the data will be read. The DataInputStream reads bytes from this underlying stream & converts them into the appropriate primitive data types or strings using the methods defined in the DataInput interface.
Java DataInputStream Class Methods
The DataInputStream class provides many methods for reading primitive data types and strings from an input stream. Let’s discuss some commonly used methods:
1. `read()`: Reads a single byte from the input stream.
int byteValue = dataInputStream.read();
2. `readFully(byte[] b)`: Reads bytes from the input stream into a byte array.
byte[] byteArray = new byte[10];
dataInputStream.readFully(byteArray);
3. `readBoolean()`: Reads a boolean value from the input stream.
boolean booleanValue = dataInputStream.readBoolean();
4. `readByte()`: Reads a signed byte value from the input stream.
byte byteValue = dataInputStream.readByte();
5. `readShort()`: Reads a signed short value (16 bits) from the input stream.
short shortValue = dataInputStream.readShort();
6. `readInt()`: Reads a signed integer value (32 bits) from the input stream.
int intValue = dataInputStream.readInt();
7. `readLong()`: Reads a signed long value (64 bits) from the input stream.
long longValue = dataInputStream.readLong();
8. `readFloat()`: Reads a float value (32 bits) from the input stream.
float floatValue = dataInputStream.readFloat();
9. `readDouble()`: Reads a double value (64 bits) from the input stream.
double doubleValue = dataInputStream.readDouble();
10. `readUTF()`: Reads a string in UTF-8 format from the input stream.
String stringValue = dataInputStream.readUTF();
These methods read data from the input stream in a specific format and return the corresponding primitive data type or string value. If an error occurs while reading from the stream or the end of the stream is reached, an IOException is thrown.
Note: It's important to remember that the data being read by these methods must have been written in the same format using the corresponding write methods of the DataOutputStream class. This ensures that the data can be accurately reconstructed when reading from the stream.
Example of DataInputStream Class
Let's take a look at an example that shows how to use the DataInputStream class to read data from a file:
import java.io.*;
public class DataInputStreamExample {
public static void main(String[] args) {
try {
// Create a FileInputStream
FileInputStream fileInputStream = new FileInputStream("data.bin");
// Create a DataInputStream
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
// Read data from the input stream
int intValue = dataInputStream.readInt();
double doubleValue = dataInputStream.readDouble();
boolean booleanValue = dataInputStream.readBoolean();
String stringValue = dataInputStream.readUTF();
// Print the read values
System.out.println("Int Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
System.out.println("String Value: " + stringValue);
// Close the input stream
dataInputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
java.io.FileNotFoundException: data.bin (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at DataInputStreamExample.main(DataInputStreamExample.java:8)
In this example:
1. We create a FileInputStream to read data from a file named "data.bin".
2. We create a DataInputStream by passing the FileInputStream to its constructor. This allows us to read primitive data types and strings from the file.
3. We use the appropriate read methods of DataInputStream to read an int, double, boolean, & string value from the file.
4. We print the read values to the console.
5. Finally, we close the DataInputStream & the FileInputStream to release any resources associated with them.
Note: In order to make this example work, the "data.bin" file must contain data written in the same format using a DataOutputStream. If the file doesn't exist or contains data in a different format, an IOException will be thrown.
Frequently Asked Questions
Can DataInputStream read data from any type of input stream?
Yes, DataInputStream can read data from any input stream that extends the InputStream class, such as FileInputStream or ByteArrayInputStream.
Is DataInputStream thread-safe?
No, DataInputStream is not thread-safe. If multiple threads need to read from the same input stream concurrently, synchronization should be used.
What happens if the input stream doesn't contain data in the expected format?
If the input stream doesn't contain data in the expected format, an IOException will be thrown when trying to read the data using DataInputStream.
Conclusion
In this article, we discussed the Java DataInputStream class, which provides a convenient way to read primitive data types & strings from an input stream. We learned about its declaration, methods, & how to use it in practice. DataInputStream simplifies the process of reading data from an input stream & ensures platform independence.
You can also check out our other blogs on Code360.