Skip to content

Commit 04247ce

Browse files
committed
feat: add CharArrayReader example to read characters from array.
WHAT: - Implemented `CharArrayReaderExample` demonstrating the use of `CharArrayReader`. - Created a character array containing the English alphabet (a–z). - Used `CharArrayReader` to sequentially read characters from the array. - Printed each character to the console until end-of-stream (-1) was reached. WHY: - `CharArrayReader` is a specialized character stream in Java that allows treating a character array as a source of input (like a file or network stream). - Useful for testing, prototyping, or processing text data without needing actual I/O resources. HOW: 1. Defined a `char[]` array with values 'a' through 'z'. 2. Initialized `CharArrayReader` with the array, making it behave like a reader stream. 3. Invoked `read()` in a loop until `-1` (EOF) was returned. 4. Printed characters to verify proper reading. REAL-WORLD USE CASES: - Simulating file or network input when working with in-memory character data. - Parsing strings or character arrays using APIs that expect a `Reader`. - Testing algorithms that work on `Reader` streams without needing files. - Useful in scenarios like templating engines or XML/JSON parsers where in-memory text is processed as if it were a stream. NOTES: - Unlike `FileReader`, `CharArrayReader` does not involve any disk I/O. - Supports `mark()` and `reset()` operations, allowing re-reading of sections. - Often paired with `CharArrayWriter` when both reading and writing in-memory character data is required. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7ff578c commit 04247ce

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Section23JavaIOStreams/src/CharArrayReaderExample.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,31 @@ public static void main(String[] args)throws Exception {
88
System.out.print(" "+(char)x);
99
}
1010
}
11-
}
11+
}
12+
13+
/*
14+
1. CharArrayReader:
15+
- Ye ek subclass hai Reader ka (java.io.Reader).
16+
- Ek `char[]` ko input stream ke tarah treat karta hai.
17+
- Matlab file ke jagah directly ek array se read kar sakte ho.
18+
19+
2. Constructor:
20+
- `CharArrayReader(char[] buf)` → poora character array ko Reader banata hai.
21+
- `CharArrayReader(char[] buf, int offset, int length)` → sirf ek portion ko Reader banata hai.
22+
23+
3. Code Flow:
24+
- Ek char array `{'a','b',...,'z'}` banaya.
25+
- `CharArrayReader car = new CharArrayReader(c);` → ab car stream jaisa behave karega.
26+
- `car.read()` ek ek karke characters deta hai (int return hota hai, isliye typecast `(char)x` kiya).
27+
- Jab saare characters read ho jaye → `read()` -1 return karega.
28+
29+
4. Use Cases:
30+
- Jab tumhare paas already memory me `char[]` data ho aur tum usko stream ke tarah process karna chahte ho.
31+
- FileReader jaisa hi use hota hai, par source file ke bajay in-memory array hota hai.
32+
- Useful testing ke liye (jaise parsers, scanners, etc.).
33+
34+
35+
✔ CharArrayReader ek char[] ko Reader banata hai.
36+
✔ read() ek ek char deta hai jab tak array khatam na ho.
37+
✔ FileReader ka in-memory alternative.
38+
*/

0 commit comments

Comments
 (0)