Explain Codes LogoExplain Codes Logo

What is InputStream & OutputStream? Why and when do we use them?

java
io-operations
stream-decorator
buffering
Alex KataevbyAlex Kataev·Mar 11, 2025
TLDR

InputStream is a Java class that provides methods for reading bytes from a binary source, such as a file or a network socket.

InputStream is = new FileInputStream("input.txt"); int byteData = is.read(); // Good, old fashioned byte sucking

On the other hand, OutputStream is a Java class that provides methods for writing bytes to a binary target, such as a file or a network socket.

OutputStream os = new FileOutputStream("output.txt"); os.write(65); // Venturing into the world of bytes with 'A'

Finally, if you need to read data, you use InputStream; if you want to write data, you use OutputStream.

Stream decoration: the art of wrapping

Buffer for efficiency

BufferedInputStream and BufferedOutputStream are best friends with base streams, reducing the number of I/O operations like real wingmen.

InputStream is = new BufferedInputStream(new FileInputStream("input.txt")); OutputStream os = new BufferedOutputStream(new FileOutputStream("output.txt"));

Primitive data types and strings

Push DataInputStream or DataOutputStream into battle when it's time to handle primitives and strings.

DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin")); dos.writeInt(123); // Because who writes 'A' in binary file? dos.writeUTF("Hello"); // Hey! Strings are also welcome DataInputStream dis = new DataInputStream(new FileInputStream("data.bin")); int number = dis.readInt(); String greeting = dis.readUTF();

Crafting with objects

For serializable objects, call up ObjectOutputStream and ObjectInputStream.

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.dat")); oos.writeObject(myObject); // I feel like a magician ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat")); MyClass object = (MyClass) ois.readObject(); // Abracadabra!

Catering for different data sources

File operations

FileInputStream and FileOutputStream is for file operations—the real show stoppers.

FileInputStream fis = new FileInputStream("photo.jpg"); FileOutputStream fos = new FileOutputStream("copy_photo.jpg"); // Let's clone this picture!

Character Stream I/O

FileReader and FileWriter — the charmers for characters instead of bytes.

Reader reader = new FileReader("text.txt"); Writer writer = new FileWriter("new_text.txt"); // Writing characters like a poet

Custom streams

For those unique situations, custom stream for unique data format.

InputStream customStream = new CustomInputStream(...); OutputStream customStream = new CustomOutputStream(...); // Because 'standard' is too mainstream

Special formats

Formatting is everything, ZipInputStream and ZipOutputStream are all about special formats.

ZipInputStream zis = new ZipInputStream(new FileInputStream("archive.zip")); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("compressed.zip")); // Zipping like there's no tomorrow

Best practices

Choose the right tool

Pick the most suitable stream type based on the task at hand. Readability and speed do matter!

Not all at once

Marco Polo of data — Discovering data in chunks is better than uncovering all at once, especially for large files.

Memory management

Don't forget to close your streams. Orphans are never good news, even in the world of streams.

try (InputStream is = new FileInputStream("input.txt")) { // Read data } //Stream: "I'm done, closing up!"