Java IO
The java.io package is Java's original input/output library. It is built on streams—sequences of data flowing into a program (input streams) or out of it...
Streams, Readers, and Writers in java.io
The java.io package is Java's original input/output library. It is built on streams—sequences of data flowing into a program (input streams) or out of it (output streams). Every stream is either a byte stream, dealing in raw 8-bit bytes, or a character stream, dealing in 16-bit Unicode characters. Understanding the package means understanding this two-by-two matrix of (byte vs character) crossed with (input vs output) and knowing which concrete classes fill each cell.
Byte Streams: The Foundation InputStream and OutputStream are the abstract roots of all byte streams. Concrete subclasses include FileInputStream (reads from a file), FileOutputStream (writes to a file), ByteArrayInputStream (reads from an in-memory byte array), and PipedInputStream/PipedOutputStream (connects two threads via a pipe). Byte streams are appropriate for binary data where you need exact byte control: image files, compressed archives, network packets, or serialized Java objects.
Character Streams: Text with Encoding Reader and Writer are the abstract roots of character streams. FileReader wraps a FileInputStream and decodes bytes to characters using the platform's default charset (or a specified one). FileWriter encodes characters to bytes using the same charset. InputStreamReader and OutputStreamWriter act as bridges, connecting byte streams to character streams with explicit charset control. Always specify the charset explicitly—using the platform default can cause bugs when code moves between machines with different locale settings.
Decorator Pattern in java.io java.io uses the Decorator design pattern extensively. Bare stream classes (FileInputStream, FileReader) provide basic access. Decorator classes add features: BufferedInputStream adds an internal byte buffer, DataInputStream adds methods to read primitives (readInt, readDouble), GZIPInputStream decompresses on the fly, CipherInputStream decrypts, and ObjectInputStream deserializes Java objects. You can stack decorators: new BufferedInputStream(new GZIPInputStream(new FileInputStream("file.gz"))) reads a compressed file with buffering.
Code Example: // Read binary file into byte array try (FileInputStream fis = new FileInputStream("image.png"); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte[] buf = new byte[4096]; int n; while ((n = fis.read(buf)) != -1) { baos.write(buf, 0, n); } byte[] imageBytes = baos.toByteArray(); System.out.println("Read " + imageBytes.length + " bytes"); }
Interview Tip: Be ready to explain the Decorator pattern in the context of java.io. When an interviewer asks why you wrap FileInputStream in BufferedInputStream, explain that BufferedInputStream is a decorator that adds buffering behavior without changing the InputStream interface. This shows you understand not just how to use the API but why it was designed that way—a distinction that separates senior candidates from junior ones.