Java Programming Handbook

    Java I/O Streams: BufferedInputStream and BufferedOutputStream

    In this blog, we’ll dive into two important Java classes that help improve performance during file I/O operations: BufferedInputStream and BufferedOutputStream. These classes add buffering capabilities to reduce the number of disk accesses, making I/O operations more efficient.

    1. What Are BufferedInputStream and BufferedOutputStream?#

    • BufferedInputStream: Wraps an InputStream and adds a buffer to reduce read operations from the underlying source.
    • BufferedOutputStream: Wraps an OutputStream and adds a buffer to reduce write operations to the underlying destination.

    These are especially useful when working with large files or when performance is a concern.

    2. BufferedInputStream Methods (With Examples)#

    int read()#

    Reads a single byte of data. Returns -1 at the end of the file.

    Example:#

    import java.io.*; public class BufferedInputStreamReadExample { public static void main(String[] args) { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"))) { int data; while ((data = bis.read()) != -1) { // Print each character read from the file System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } } }

    Output (if input.txt contains "Buffered Stream"):#

    Buffered Stream

    int read(byte[] b, int off, int len)#

    Reads up to len bytes of data into an array b, starting at offset off.

    Example:#

    import java.io.*; public class BufferedInputStreamReadArrayExample { public static void main(String[] args) { byte[] buffer = new byte[20]; try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"))) { int bytesRead = bis.read(buffer, 0, buffer.length); System.out.println("Read " + bytesRead + " bytes: " + new String(buffer, 0, bytesRead)); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Read 16 bytes: Buffered Stream

    void close()#

    Closes the stream and releases any resources associated with it.

    Example:#

    import java.io.*; public class BufferedInputStreamCloseExample { public static void main(String[] args) { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"))) { System.out.println("Stream opened. It will be closed automatically after the try block."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Stream opened. It will be closed automatically after the try block.

    3. BufferedOutputStream Methods (With Examples)#

    void write(int b)#

    Writes a single byte to the output stream.

    Example:#

    import java.io.*; public class BufferedOutputStreamWriteExample { public static void main(String[] args) { try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) { bos.write(66); // ASCII for 'B' bos.flush(); // Ensures data is written to the file immediately System.out.println("Single byte written successfully."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Single byte written successfully.

    (File output.txt contains: B)

    void write(byte[] b, int off, int len)#

    Writes len bytes from the array b, starting at offset off.

    Example:#

    import java.io.*; public class BufferedOutputStreamWriteArrayExample { public static void main(String[] args) { String data = "Buffered Output Example"; byte[] bytes = data.getBytes(); try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) { bos.write(bytes, 0, bytes.length); // Write all bytes bos.flush(); // Push any remaining buffered data to file System.out.println("Byte array written successfully."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Byte array written successfully.

    (File output.txt contains: Buffered Output Example)

    void flush()#

    Flushes the buffered output to the file. Ensures no data is stuck in memory.

    Note: flush() is especially useful when writing to a stream that stays open, like network sockets or large files.

    void close()#

    Closes the stream and flushes any remaining data.

    Example:#

    import java.io.*; public class BufferedOutputStreamCloseExample { public static void main(String[] args) { try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) { bos.write("Closing Stream".getBytes()); System.out.println("Data written. Stream will be closed automatically."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Data written. Stream will be closed automatically.

    4. When to Use Buffered Streams?#

    • Use BufferedInputStream when reading large files or doing multiple small reads.
    • Use BufferedOutputStream when writing large files or frequent small writes.
    • Buffering reduces interaction with the file system, improving performance.

    Conclusion#

    In this blog, we learned about BufferedInputStream and BufferedOutputStream — how they enhance performance by reducing direct read/write calls to disk. We covered their key methods with practical examples

    In the next blog, we’ll explore Java PrintStream, which is commonly used for writing formatted data to console and files.

    Last updated on Apr 09, 2025