Byte Streams
Byte Streams in Java are used to read and write raw binary data (bytes). They are the foundation of Java I/O and are primarily used for non-text data such as images, audio, video, PDFs, and binary files. This is a core Java I/O concept and a common interview topic.
What Are Byte Streams?
- Work with 8-bit bytes
- Used for binary data
- Platform-independent
- Part of java.io package
- Base classes:
- InputStream
- OutputStream
When to Use Byte Streams
- ✔ Images (.jpg, .png)
- ✔ Audio / video files
- ✔ PDF, ZIP, executable files
- ✔ Network and socket data
- ❌ Not ideal for plain text (use character streams)
Byte Stream Class Hierarchy (Important)
Input Side
InputStream
├── FileInputStream
├── BufferedInputStream
├── DataInputStream
└── ObjectInputStream
Output Side
OutputStream
├── FileOutputStream
├── BufferedOutputStream
├── DataOutputStream
└── ObjectOutputStream
InputStream (Base Class)
Used to read bytes.
Key Methods
int read(); // reads one byte
int read(byte[] b); // reads multiple bytes
void close(); // closes stream
✔ Returns -1 when end of stream is reached
OutputStream (Base Class)
Used to write bytes.
Key Methods
void write(int b); // writes one byte
void write(byte[] b); // writes byte array
void flush(); // forces write
void close(); // closes stream
FileInputStream (Reading Binary Data)
FileInputStream fis = new FileInputStream("image.jpg");
int data;
while ((data = fis.read()) != -1) {
System.out.print(data);
}
fis.close();
✔ Reads byte-by-byte
❌ Slow without buffering
FileOutputStream (Writing Binary Data)
FileOutputStream fos = new FileOutputStream("copy.jpg");
fos.write(65); // writes byte
fos.close();
✔ Creates file if not exists
✔ Overwrites by default
Copying a File Using Byte Streams (Common Example)
FileInputStream fis = new FileInputStream("source.jpg");
FileOutputStream fos = new FileOutputStream("dest.jpg");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fis.close();
fos.close();
✔ Efficient
✔ Real-world usage
Buffered Byte Streams (Performance Boost)
BufferedInputStream & BufferedOutputStream
- Use internal buffer
- Reduce disk I/O operations
- Faster than plain streams
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream("a.jpg"));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream("b.jpg"));
✔ Always preferred for large files
Byte Streams vs Character Streams (Interview Table)
| Aspect | Byte Streams | Character Streams |
|---|---|---|
| Data type | Bytes | Characters |
| File type | Binary | Text |
| Base classes | InputStream / OutputStream | Reader / Writer |
| Encoding | ❌ No | ✔ Yes |
| Example | Image, PDF | Text, CSV |
Common Byte Stream Classes (Must Remember)
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
- DataInputStream
- DataOutputStream
- ObjectInputStream
- ObjectOutputStream
Common Beginner Mistakes
- Using byte streams for text files
- Forgetting to close streams
- Not using buffering
- Reading byte-by-byte unnecessarily
- Ignoring exception handling
Best Practices
- Use buffered streams for performance
- Use try-with-resources for auto-close
- Use byte streams only for binary data
- Prefer NIO (Files.copy) for modern code
try (FileInputStream fis = new FileInputStream("a.jpg");
FileOutputStream fos = new FileOutputStream("b.jpg")) {
// logic
}
Interview-Ready Answers
Short Answer
Byte streams are used to read and write binary data in Java.
Detailed Answer
In Java, byte streams are based on InputStream and OutputStream classes and are used to handle raw binary data such as images, audio, and files. They operate on bytes, are platform-independent, and form the foundation of Java I/O.
Key Takeaway
Byte Streams = Binary Data Handling.
Use them for non-text files, add buffering for performance, and prefer modern NIO APIs in production.