Skip to content

Commit 7fb5f35

Browse files
committed
feat: implement DataOutputStreamsExample22 for writing student data in binary format
WHAT: - Added `DataOutputStreamsExample22` class to demonstrate serialization using `DataOutputStream`. - Writes a student's details (roll number, name, department, average marks) into a binary file `Student2.txt`. HOW: - Used try-with-resources to safely handle `FileOutputStream` and `DataOutputStream`. - Serialized fields in the following order: 1. `writeInt(rollno)` 2. `writeUTF(name)` 3. `writeUTF(dept)` 4. `writeFloat(avg)` - Ensures the same order must be followed during deserialization. WHY: - Demonstrates **low-level manual serialization** of Java primitives and Strings. - Provides a platform-independent binary representation of structured data. - Shows safe resource management using Java’s try-with-resources. REAL-WORLD APPLICATIONS: - Storing student records, employee details, or configuration data in a lightweight binary format. - Used in games and simulations for saving state efficiently. - Provides a base for building custom serialization frameworks. - Faster and more compact than plain text storage. BENEFITS: - Auto-closing of streams avoids memory/resource leaks. - Compact binary format ensures efficiency in storage and retrieval. - Maintains data integrity across JVMs and platforms. NEXT IMPROVEMENTS: - Add a matching `DataInputStreamsExample22` to deserialize and read back student data. - Wrap serialization logic into reusable methods (e.g., `writeStudent(Student student)`). - Consider using `Serializable` interface for more scalable object persistence. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 2015cf0 commit 7fb5f35

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.*;
2+
3+
public class DataOutputStreamsExample22 {
4+
public static void main(String[] args) {
5+
String filePath = "/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Student2.txt";
6+
7+
try (FileOutputStream fos = new FileOutputStream(filePath);
8+
DataOutputStream dos = new DataOutputStream(fos))
9+
{
10+
int rollno = 101;
11+
String name = "Somesh Diwan";
12+
String dept = "Computer Science";
13+
float avg = 87.5f;
14+
15+
// Serialisation: using DataInput and DataOutput Streams.
16+
dos.writeInt(rollno);
17+
dos.writeUTF(name);
18+
dos.writeUTF(dept);
19+
dos.writeFloat(avg);
20+
21+
System.out.println("Data written successfully.");
22+
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}
28+
29+
/*
30+
1. DataInputStream:
31+
- Ye stream primitive data types (int, float, double, char, UTF strings, etc.)
32+
ko directly read/write karne ke liye hota hai.
33+
- File se data ko ek particular "binary format" me read karta hai.
34+
- Isko hamesha usi order me read karna padta hai jis order me DataOutputStream ne likha tha.
35+
36+
2. Code Explanation:
37+
- `FileInputStream fis = new FileInputStream("path")`
38+
→ File ko open karta hai.
39+
- `DataInputStream dis = new DataInputStream(fis)`
40+
→ Primitive data types ko directly read karne ka wrapper stream.
41+
42+
3. Object `Student1`:
43+
- Ek custom class jisme rollno, name, dept, avg fields hain.
44+
- Yahan tum binary file me saved student data ko read kar rahe ho.
45+
46+
4. Read karne ka order:
47+
- `dis.readInt()` → integer read karega.
48+
- `dis.readUTF()` → ek UTF string read karega.
49+
- `dis.readUTF()` → second UTF string read karega.
50+
- `dis.readFloat()` → float read karega.
51+
- Order **must match exactly** with the order used while writing using DataOutputStream.
52+
Agar order change hua to data corrupt lagega ya exception throw hoga.
53+
54+
5. Output:
55+
- Roll No, Name, Average, Dept console par print hoga.
56+
- Example:
57+
```
58+
Roll No: 101
59+
Name: Somesh
60+
Average: 85.5
61+
Dept: CS
62+
```
63+
64+
6. Important Points:
65+
- Tumhare code me `dis.close(); dis.close();` do baar likha hai → ek ko hata do.
66+
- Always ensure matching order between DataOutputStream (writer) and DataInputStream (reader).
67+
- Ye serialisation ka ek simpler form hai → but ye Java’s built-in `Serializable` interface se different hai.
68+
- Agar multiple objects likhne hain → loop use karke likho aur read karte waqt same loop repeat karo.
69+
70+
71+
- Use `try-with-resources` to auto-close streams (Java 7+).
72+
- Pehle ek `DataOutputStream` program likho jo Student data file me likhe,
73+
fir usko iss program se read karke verify karo.
74+
- Agar tumhe complex objects store karne hain to prefer `ObjectOutputStream` / `ObjectInputStream`.
75+
*/

0 commit comments

Comments
 (0)