Skip to content

Commit 01d0c52

Browse files
committed
feat: add FileExample4 to demonstrate reading files with FileReader (character stream)
WHAT: - Implemented FileExample4 to show how to read text files using FileReader instead of FileInputStream. - Used try-with-resources for automatic resource management. - Read the file character by character until end-of-file (`-1`) and printed the output. WHY: - FileReader is a character stream, making it more suitable for reading text files compared to byte streams. - Automatically handles character decoding based on the platform’s default charset. - Provides a simpler approach for text data compared to manually casting bytes from FileInputStream. HOW: 1. Opened the file `WithResources.txt` using FileReader inside try-with-resources. 2. Used `fr.read()` to read one character at a time. 3. Printed each character to the console until EOF was reached. BENEFITS: - Demonstrates character-based file reading in Java. - Simplifies handling of text files without manual byte-to-char conversion. - Ensures proper resource cleanup using try-with-resources. REAL-WORLD APPLICATIONS: - Reading configuration files, logs, or structured text data. - Building simple text processing tools where character decoding is automatically managed. - Useful in situations where only text files (not binary data) are processed. NOTES: - For larger files, `BufferedReader` should be preferred over FileReader alone for efficiency. - Default charset is platform-dependent; for consistent cross-platform behavior, use `InputStreamReader` with explicit charset. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent f9d213a commit 01d0c52

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

Section23JavaIOStreams/src/FileExample4.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
22

33
public class FileExample4 {
44
public static void main(String[] args)throws Exception {
5-
/*Using Reader and Writer*/
6-
try(FileReader fr = new FileReader("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA/WithResources.txt");)
5+
/* Using Reader and Writer */
6+
try(FileReader fr = new FileReader("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/WithResources.txt");)
77
{
88
int x;
9-
while((x=fr.read())!=-1)
10-
{
9+
while((x=fr.read()) != -1) {
1110
System.out.print((char)x);
1211
}
1312
}
1413
}
15-
}
14+
}
15+
16+
/*
17+
1. FileReader:
18+
- Ye ek character-based input stream hai (text files read karne ke liye best).
19+
- Unlike FileInputStream (jo binary/byte read karta hai), FileReader directly characters read karta hai.
20+
- Encoding (default platform encoding) handle karta hai → text file ke liye safer option.
21+
22+
2. Important Methods:
23+
- fr.read() → ek single character return karega (int form me, -1 if EOF).
24+
- fr.read(char[] cbuf) → ek baar me multiple characters ek array me read karega.
25+
- fr.close() → stream ko close karta hai (but try-with-resources me auto close ho jaata hai).
26+
27+
3. is program me kya ho raha hai:
28+
- FileReader `WithResources.txt` ko open karta hai.
29+
- While loop me `fr.read()` se ek-ek character read ho raha hai jab tak -1 (EOF) nahi milta.
30+
- `(char)x` cast karke usko console pe print kiya gaya hai.
31+
32+
4. try-with-resources:
33+
- Yaha `try(FileReader fr = new FileReader("path"))` likha gaya hai.
34+
- Iska fayda: fr.close() automatically call ho jaata hai jab try block finish hota hai.
35+
36+
5. Use Cases:
37+
- Text files read karna (letters, words, sentences).
38+
- Jab tumhe encoding safe character stream chahiye.
39+
- Agar tumhe binary data (images, pdf, audio) read karna ho toh `FileInputStream` use karo.
40+
41+
✔ FileReader = character input stream.
42+
✔ read() → char by char data read karta hai.
43+
✔ Best for text files (not binary).
44+
✔ try-with-resources = auto close, no memory leaks.
45+
*/

0 commit comments

Comments
 (0)