Skip to content

Commit 9604c5d

Browse files
authored
Update complete-java21-qa.md
1 parent 808634e commit 9604c5d

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

complete-java21-qa.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,3 +1517,124 @@ family.ages().put("Dad", 45); // Compiles but violates immutability intent
15171517
```
15181518

15191519
**Best Practice:** Use `Map.copyOf()`, `List.copyOf()`, or `Set.copyOf()` in constructors for true immutability with mutable types.
1520+
1521+
---
1522+
1523+
### Q67: What are the key differences between binary streams, character streams, and bridge streams in Java I/O?
1524+
1525+
**Answer:**
1526+
1527+
**Binary Streams vs Character Streams:**
1528+
1529+
**PrintStream (Binary Stream):**
1530+
- Writes **binary data** (bytes) to destination
1531+
- Example: `System.out` (console output)
1532+
- Converts input to bytes using charset/encoding
1533+
- Methods do **NOT throw checked exceptions** (unique feature)
1534+
```java
1535+
PrintStream ps = System.out;
1536+
ps.println("Hello"); // Converts to bytes, no IOException
1537+
```
1538+
1539+
**Character Streams:**
1540+
- Work with **characters** instead of bytes
1541+
- Examples: `Reader`, `Writer`, `BufferedReader`, `BufferedWriter`
1542+
- Better for text processing
1543+
1544+
**Bridge Streams:**
1545+
1546+
**Purpose:** Convert between binary and character streams
1547+
1548+
**InputStreamReader / OutputStreamWriter:**
1549+
```java
1550+
// Bridge from binary (InputStream) to character (Reader)
1551+
FileInputStream fis = new FileInputStream("file.txt");
1552+
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
1553+
BufferedReader br = new BufferedReader(isr); // Character stream
1554+
1555+
// Bridge from character (Writer) to binary (OutputStream)
1556+
FileOutputStream fos = new FileOutputStream("file.txt");
1557+
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
1558+
BufferedWriter bw = new BufferedWriter(osw); // Character stream
1559+
```
1560+
1561+
**PrintWriter (Special Case):**
1562+
- Character stream class
1563+
- Creates bridge internally (no manual bridge needed)
1564+
```java
1565+
PrintWriter pw = new PrintWriter("file.txt"); // No bridge needed
1566+
pw.println("Hello"); // Works directly with characters
1567+
```
1568+
1569+
**Reader.read(char[] buffer) Method:**
1570+
1571+
**Behavior:**
1572+
- Reads characters equal to (or less than) buffer size
1573+
- Returns number of characters actually read
1574+
- Returns **-1** when end of file reached
1575+
- **Critical:** Buffer may contain leftover data from previous reads
1576+
1577+
**Standard Pattern:**
1578+
```java
1579+
char[] buffer = new char[1024];
1580+
int count;
1581+
1582+
while ((count = reader.read(buffer)) != -1) {
1583+
// Use only 'count' characters, not entire buffer
1584+
String data = new String(buffer, 0, count);
1585+
// Process data
1586+
}
1587+
```
1588+
1589+
**Why this matters:**
1590+
```java
1591+
// ❌ Wrong - may include junk from previous read
1592+
char[] buffer = new char[10];
1593+
reader.read(buffer); // Reads 5 chars, buffer has 5 leftover
1594+
String wrong = new String(buffer); // Includes 5 junk chars!
1595+
1596+
// ✅ Correct - use only what was read
1597+
int count = reader.read(buffer);
1598+
String correct = new String(buffer, 0, count); // Only 5 chars
1599+
```
1600+
1601+
**Writer.write(char[] buffer) Methods:**
1602+
1603+
**Two versions:**
1604+
1605+
1. **write(char[] buffer)** - writes entire buffer
1606+
```java
1607+
writer.write(buffer); // Writes ALL characters, including junk
1608+
```
1609+
1610+
2. **write(char[] buffer, int offset, int length)** - writes specific range
1611+
```java
1612+
int count = reader.read(buffer);
1613+
writer.write(buffer, 0, count); // ✅ Writes only what was read
1614+
```
1615+
1616+
**Best Practice Pattern:**
1617+
```java
1618+
char[] buffer = new char[1024];
1619+
int count;
1620+
1621+
while ((count = reader.read(buffer)) != -1) {
1622+
writer.write(buffer, 0, count); // Write only valid data
1623+
}
1624+
```
1625+
1626+
**Key Takeaways:**
1627+
1628+
| Stream Type | Handles | Encoding | Checked Exceptions |
1629+
|-------------|---------|----------|-------------------|
1630+
| Binary (InputStream/OutputStream) | Bytes | Manual | Yes (IOException) |
1631+
| Character (Reader/Writer) | Characters | Automatic | Yes (IOException) |
1632+
| PrintStream | Bytes (but looks like characters) | Automatic | **No** |
1633+
| Bridge (InputStreamReader) | ByteCharacter conversion | Specified | Yes (IOException) |
1634+
| PrintWriter | Characters | Automatic (bridge internal) | **No** |
1635+
1636+
**Common Pitfalls:**
1637+
-Using entire buffer without checking read count
1638+
-Forgetting that PrintStream doesn't throw checked exceptions
1639+
- ❌ Not specifying encoding for InputStreamReader/OutputStreamWriter
1640+
- ❌ Using `write(buffer)` instead of `write(buffer, 0, count)`

0 commit comments

Comments
 (0)