Commit b5d3a21
committed
feat(io-byte-streams): implement file copy using FileInputStream and FileOutputStream
What
- Added `Test.java` demonstrating how to use Java byte streams (`FileInputStream`, `FileOutputStream`) to copy file contents.
- Reads raw bytes from `input.txt` and writes them into `output.txt` one byte at a time.
- Wrapped logic in try-catch-finally with proper resource management (manual closing of streams).
- Included inline revision notes summarizing:
- Definition of byte streams.
- Core classes (`FileInputStream`, `FileOutputStream`).
- Code flow (`.read()`, `.write()` until EOF).
- Real-world applications (copying files, network sockets, compressed formats).
- Best practices (closing streams, try-with-resources).
Why
- Byte streams are the foundation of Java I/O for handling raw binary data.
- Many real-world applications (multimedia, networking, serialization) depend on efficient binary handling.
- This example provides a minimal yet complete demonstration of copying files at the byte level.
- Reinforces understanding of the `InputStream` / `OutputStream` hierarchy and low-level I/O mechanics.
Logic
1. **Setup**
- Defined input (`input.txt`) and output (`output.txt`) file paths.
- Initialized `FileInputStream` for reading and `FileOutputStream` for writing.
2. **Processing**
- Loop reads bytes using `fis.read()`.
- If return value != -1 (EOF marker), the byte is written to the output stream with `fos.write(byteData)`.
3. **Termination**
- Loop ends at EOF (`-1`).
- Streams are closed in `finally` to ensure resource cleanup regardless of exceptions.
4. **Error Handling**
- `IOException` caught and stack trace printed.
- Nested try-catch in `finally` ensures safe closing of both input and output streams.
Real-life applications
- **File Copy Utilities**: Low-level implementation for copying images, PDFs, executables.
- **Networking**: Transfer raw byte streams across sockets (e.g., HTTP, FTP).
- **Media Processing**: Reading/writing audio/video in raw format.
- **Compression**: Handling zipped or encrypted binary formats.
Notes
- `.read()` returns an `int` (0–255) or `-1` for EOF → important for correct loop termination.
- Streams must be closed to free system resources; modern Java prefers `try-with-resources`.
- Byte streams are efficient for binary data, while character streams (`Reader`, `Writer`) are better for text-based data.
- This example demonstrates sequential, single-byte I/O; buffered streams (`BufferedInputStream`, `BufferedOutputStream`) can improve performance for large files.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7bce761 commit b5d3a21
File tree
1 file changed
+76
-0
lines changed- Java 8 Crash Course/Java 8 Streams/Byte Streams/src
1 file changed
+76
-0
lines changedLines changed: 76 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
0 commit comments