Commit e9e5eea
committed
feat: add PrintStream-based student record writer
WHAT:
- Implemented `PrintStreamDemo` to demonstrate writing structured student data into a text file.
- Used `PrintStream` for writing primitive values and strings in a human-readable format.
HOW:
- Created `Student` class with fields: rollno, name, dept.
- Instantiated a `Student` object and populated its fields.
- Used `PrintStream` methods (`println()`) to write the student's roll number, name, and department into `Student1.txt`.
- Closed resources (`ps`, `fos`) to avoid memory leaks.
WHY:
- Showcases the difference between binary I/O (`DataOutputStream`) and text-based I/O (`PrintStream`).
- `PrintStream` provides convenient `print()` and `println()` methods similar to `System.out`, making it ideal for logging and text file writing.
- Output file is human-readable, unlike binary streams.
REAL-WORLD APPLICATIONS:
- Writing logs, reports, or configuration files where human readability is important.
- Exporting application data (like student records, employee details) in plain text format.
- Useful for quick debugging by persisting object state into a readable file.
NOTES:
- Unlike `DataOutputStream`, `PrintStream` writes data as text rather than binary.
- The order of writing still matters if another program will parse this file later.
- `PrintStream` does not throw `IOException` directly; instead, you should check for errors using `ps.checkError()` if required.
NEXT IMPROVEMENTS:
- Add a constructor to `Student` class for easier initialization.
- Wrap file operations in `try-with-resources` for automatic closing.
- Extend program to handle multiple student records (possibly using a loop or list).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 26e4e7c commit e9e5eea
File tree
2 files changed
+11
-11
lines changed- Section23JavaIOStreams
- Serialisation Storing Data in a File/src
- src/MyJAVA
2 files changed
+11
-11
lines changedLines changed: 11 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
Binary file not shown.
0 commit comments