🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The DataInputStream class is a filter stream that reads data from an input stream in a binary format. This class is essential for reading primitive data types (such as int, float, double, etc.) from a stream in a way that ensures the data is interpreted correctly regardless of the platform. By providing methods to read Java's primitive data types, DataInputStream makes it easier to handle binary I/O operations.
Table of Contents
- Creating a DataInputStream
- Reading Data from a DataInputStream
- Closing the Stream
- Complete Example
- Conclusion
Creating a DataInputStream
To create a DataInputStream, you need to wrap it around another input stream, such as a FileInputStream. This enables you to read data from a file or another input source in a binary format.
Example
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class DataInputStreamExample {
public static void main(String[] args) {
try {
// Creating DataInputStream using FileInputStream
FileInputStream fis = new FileInputStream("example.dat");
DataInputStream dis = new DataInputStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
In the example above, DataInputStream is created using a FileInputStream to read from a file named example.dat.
Reading Data from a DataInputStream
The DataInputStream class provides several methods to read primitive data types from the input stream:
boolean readBoolean(): Reads a boolean value.byte readByte(): Reads an 8-bit byte.char readChar(): Reads a 16-bit character.double readDouble(): Reads a 64-bit double.float readFloat(): Reads a 32-bit float.int readInt(): Reads a 32-bit integer.long readLong(): Reads a 64-bit long.short readShort(): Reads a 16-bit short.
Example
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamReadExample {
public static void main(String[] args) {
try (DataInputStream dis = new DataInputStream(new FileInputStream("example.dat"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, DataInputStream reads data from a file named example.dat. The file is expected to contain an integer, a double, and a boolean value in sequence.
Closing the Stream
It is important to close the DataInputStream after completing the I/O operations to release the system resources associated with the stream. This can be done using the close() method.
Example
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamCloseExample {
public static void main(String[] args) {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("example.dat"));
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
In the example above, the DataInputStream is closed in the finally block to ensure that it is closed even if an exception occurs.
Alternatively, you can use the try-with-resources statement, which ensures that the stream is closed automatically.
Example
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamTryWithResourcesExample {
public static void main(String[] args) {
try (DataInputStream dis = new DataInputStream(new FileInputStream("example.dat"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, the try-with-resources statement is used to automatically close the DataInputStream.
Complete Example
Here is a complete example demonstrating the creation, reading, and closing of a DataInputStream.
DataInputStreamExample.java
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamExample {
public static void main(String[] args) {
try (DataInputStream dis = new DataInputStream(new FileInputStream("example.dat"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, a DataInputStream is created, data is read from a file, and the stream is automatically closed using the try-with-resources statement.
Conclusion
The DataInputStream class in Java is used for reading primitive data types from an input stream in a machine-independent way. By understanding how to create, read, and close a DataInputStream, you can effectively handle binary I/O operations in your Java applications. Remember to always close the stream after use to ensure that system resources are properly released.
Comments
Post a Comment
Leave Comment