🎓 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment