Skip to content

Commit b5d3a21

Browse files
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 changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.FileInputStream;
2+
import java.io.FileOutputStream;
3+
import java.io.IOException;
4+
5+
public class Test {
6+
public static void main(String[] args) {
7+
// File paths (update as per your system)
8+
String inputFile = "input.txt";
9+
String outputFile = "output.txt";
10+
11+
FileInputStream fis = null;
12+
FileOutputStream fos = null;
13+
14+
try {
15+
// Create FileInputStream to read bytes from a file
16+
fis = new FileInputStream(inputFile);
17+
18+
// Create FileOutputStream to write bytes to another file
19+
fos = new FileOutputStream(outputFile);
20+
21+
int byteData;
22+
// Read one byte at a time (-1 means end of a file)
23+
while ((byteData = fis.read()) != -1) {
24+
// Write the byte into an output file
25+
fos.write(byteData);
26+
}
27+
28+
System.out.println("File copied successfully using Byte Streams!");
29+
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
} finally {
33+
try {
34+
if (fis != null) fis.close();
35+
if (fos != null) fos.close();
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
}
41+
}
42+
43+
/*
44+
Byte Streams in Java – Revision Notes
45+
46+
1. Definition:
47+
- Byte Streams in Java handle **raw binary data** (8-bit data).
48+
- They use `InputStream` and `OutputStream` as base classes.
49+
- Suitable for files like images, videos, audio, and text.
50+
51+
2. Key Classes:
52+
- FileInputStream → Reads raw bytes from a file.
53+
- FileOutputStream → Writes raw bytes into a file.
54+
55+
3. Code Flow:
56+
- `new FileInputStream("input.txt")` → opens file for reading.
57+
- `new FileOutputStream("output.txt")` → opens file for writing.
58+
- `.read()` → reads 1 byte at a time (returns int in range 0–255).
59+
- `.write(byte)` → writes 1 byte into file.
60+
- Loop continues until `.read()` returns -1 (end of file).
61+
62+
4. Real-World Use:
63+
✔ Copying binary files (images, PDFs, executables).
64+
✔ Sending/receiving data over network sockets.
65+
✔ Reading/writing compressed files.
66+
67+
5. Important:
68+
- Always close streams (`.close()`) to free system resources.
69+
- Use `try-with-resources` in modern Java for automatic closing.
70+
71+
Quick Recap:
72+
✔ Byte Streams = binary data handling.
73+
✔ Use FileInputStream + FileOutputStream.
74+
✔ `.read()` and `.write()` work byte-by-byte.
75+
✔ Good for multimedia files and raw data transfer.
76+
*/

0 commit comments

Comments
 (0)