🎓 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
Data streams in Java are used to read and write primitive data types (such as int, float, double, etc.) and String values in a machine-independent way. They provide methods for input and output of these data types, ensuring that the data can be read back in the same way it was written, regardless of the platform.
Table of Contents
- Introduction
- Data Stream Classes
- DataInputStream
- DataOutputStream
- Examples
- Writing Data to a File
- Reading Data from a File
- Copying Data Using Data Streams
- Conclusion
Introduction
Data streams are designed for reading and writing primitive data types and strings in a binary format. The primary classes for data streams are DataInputStream and DataOutputStream. These classes work with underlying byte streams (InputStream and OutputStream) to handle the input and output of data in a binary format.
Data Stream Classes
DataInputStream
The DataInputStream class allows an application to read primitive data types from an underlying input stream in a machine-independent way.
Common Methods:
readBoolean()readByte()readChar()readDouble()readFloat()readInt()readLong()readShort()readUTF()
DataOutputStream
The DataOutputStream class allows an application to write primitive data types to an output stream in a machine-independent way.
Common Methods:
writeBoolean(boolean v)writeByte(int v)writeChar(int v)writeDouble(double v)writeFloat(float v)writeInt(int v)writeLong(long v)writeShort(int v)writeUTF(String s)
Examples
Writing Data to a File
The following example demonstrates how to write primitive data types and a string to a file using DataOutputStream.
Example
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamExample {
public static void main(String[] args) {
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"))) {
dos.writeInt(123);
dos.writeDouble(45.67);
dos.writeBoolean(true);
dos.writeUTF("Hello, World!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
DataOutputStreamis used to write data to a file nameddata.dat.- The
writeInt(),writeDouble(),writeBoolean(), andwriteUTF()methods write the respective data types to the file.
Reading Data from a File
The following example demonstrates how to read the data written in the previous example using DataInputStream.
Example
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("data.dat"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
String stringValue = dis.readUTF();
System.out.println("Read values: " + intValue + ", " + doubleValue + ", " + booleanValue + ", " + stringValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
DataInputStreamis used to read data from a file nameddata.dat.- The
readInt(),readDouble(),readBoolean(), andreadUTF()methods read the respective data types from the file. - The values are printed to the console.
Copying Data Using Data Streams
The following example demonstrates how to copy data from one file to another using DataInputStream and DataOutputStream.
Example
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreamCopyExample {
public static void main(String[] args) {
try (DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("copy.dat"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
boolean booleanValue = dis.readBoolean();
String stringValue = dis.readUTF();
dos.writeInt(intValue);
dos.writeDouble(doubleValue);
dos.writeBoolean(booleanValue);
dos.writeUTF(stringValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
DataInputStreamreads data fromdata.dat.DataOutputStreamwrites data tocopy.dat.- The data read from the input file is written to the output file, effectively copying the data.
Conclusion
Data streams in Java provide a way to read and write primitive data types and strings in a binary format. They ensure that data can be read back in the same way it was written, regardless of the platform. By understanding how to use DataInputStream and DataOutputStream, you can efficiently manage and process binary data in your Java applications. The examples provided demonstrate basic usage, including writing data to a file, reading data from a file, and copying data using data streams.
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