Skip to content

Commit e9e5eea

Browse files
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

2 files changed

+11
-11
lines changed

Section23JavaIOStreams/Serialisation Storing Data in a File/src/PrintStreamExample.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import java.io.*;
2-
class Student
3-
{
2+
3+
class Student {
44
int rollno;
55
String name;
66
String dept;
77

8-
/* // Constructor to initialize Student object
8+
/* Constructor to initialize Student object
99
public Student(int rollno, String name, String dept)
1010
{
1111
this.rollno = rollno;
1212
this.name = name;
1313
this.dept = dept;
14-
}*/
14+
}
15+
*/
1516
}
16-
class PrintStreamDemo
17-
{
18-
public static void main(String[] args) throws Exception
19-
{
20-
FileOutputStream fos=new FileOutputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Student1.txt");
21-
PrintStream ps=new PrintStream(fos);
17+
18+
class PrintStreamDemo {
19+
public static void main(String[] args) throws Exception {
20+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Student1.txt");
21+
PrintStream ps = new PrintStream(fos);
2222

2323
Student s=new Student();
2424
s.rollno=10;
@@ -38,4 +38,4 @@ public static void main(String[] args) throws Exception
3838
ps.close();
3939
fos.close();
4040
}
41-
}
41+
}
-7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)