Binary Data vs Text Data
You'll learn why byte streams handle all data as raw bytes and when to choose byte streams over character streams.
The Core Difference
Every file on your computer is ultimately stored as a sequence of bytes (numbers 0-255). However, not all files are the same:
-
Binary data consists of raw bytes that represent non-textual information: images (
.jpg,.png), videos, compiled programs (.classfiles), ZIP archives, or serialized objects. These bytes have meaning specific to their format. -
Text data consists of bytes that represent human-readable characters encoded using a character set (like UTF-8 or ASCII). Each byte (or sequence of bytes) maps to a letter, number, or symbol.
Why Byte Streams Treat Everything as Binary
The InputStream and OutputStream classes you've been using operate at the byte level. They don't interpret the bytes—they just move them. When you call read(), you get a raw byte value (0-255). The stream doesn't know or care whether that byte represents part of an image pixel or the letter 'A'.
This is like a postal worker moving packages: they don't need to know what's inside to do their job.
When to Use Byte Streams vs Character Streams
Use byte streams (FileInputStream, FileOutputStream) when:
- Copying any file regardless of content
- Working with binary formats (images, audio, archives)
- You need exact byte-for-byte accuracy
Use character streams (which you'll learn soon) when:
- Reading or writing text files
- You need character encoding/decoding (UTF-8, etc.)
- Processing human-readable content line-by-line
Key Takeaway: Byte streams treat all data as raw binary bytes without interpretation, making them essential for non-text files, while character streams (coming soon) handle text encoding properly for human-readable content.