o7planning

Java BufferedOutputStream Tutorial with Examples

  1. BufferedOutputStream
  2. Examples

1. BufferedOutputStream

BufferedOutputStream is a subclass of OutputStream, which is used to simplify writing text to a binary output stream, and improve program performance.
Operating principle of BufferedOutputStream looks like the following illustration:
BufferedOutputStream wraps inside it an OutputStream object, which is responsible for writing data to the target (such as file).
BufferedOutputStream overrides methods inherited from its parent class, such as write(), write(byte[]),... to ensure that data will be written to buffer and not to target (eg file). But when buffer is full, all of the data in buffer will be pushed into OutputStream and free buffer. You can also call BufferedOutputStream.flush() method to actively push all the data on buffer to OutputStream and clear buffer. Data is also pushed from buffer to OutputStream when calling BufferedOutputStream.close() method.
The so-called "buffer" mentioned above is actually just a byte array. You can specify the size of this array when initializing BufferedOutputStream object.
Data that is temporarily written to buffer minimizes continuous writing to target (eg files on a hard drive), thus it enhances program performance.
BufferedOutputStream constructors
BufferedOutputStream(OutputStream out)    

BufferedOutputStream(OutputStream out, int size)
  • The BufferedOutputStream(OutputStream,int) constructor creates a BufferedOutputStream object with buffer array of specified size.
  • The BufferedOutputStream(OutputStream) constructor creates a BufferedOutputStream object with buffer array of default size (size = 8192).

2. Examples

Example: Create a BufferedOutputStream with buffer array size of 16384, which means this buffer is 16384 bytes (16 KB) in size.
File outFile = new File("/Volumes/Data/test/outfile.txt");
outFile.getParentFile().mkdirs(); // Create parent folder.

// Create OutputStream to write a file.
OutputStream os = new FileOutputStream(outFile);

// Create a BufferedOutputStream with buffer array size of 16384 (16384 bytes = 16 KB).
BufferedOutputStream br = new BufferedOutputStream(os, 16384);
Create a BufferedOutputStream with default buffer array size (8192), which is equivalent to 8192 bytes (8 KB).
File outFile = new File("/Volumes/Data/test/outfile.txt");
outFile.getParentFile().mkdirs(); // Create parent folder.

// Create OutputStream to write a file.
OutputStream os = new FileOutputStream(outFile);

// Create a BufferedOutputStream with default buffer array size of 8192 (8192 bytes = 8 KB).
BufferedOutputStream br = new BufferedOutputStream(os);
Full code of example:
BufferedOutputStreamEx1.java
package org.o7planning.bufferedoutputstream.ex;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BufferedOutputStreamEx1 {

    private static final String file_path = "/Volumes/Data/test/outfile.txt";

    public static void main(String[] args) throws IOException {
        File outFile = new File(file_path);
        outFile.getParentFile().mkdirs(); // Create parent folder.

        // Create OutputStream to write a file.
        OutputStream os = new FileOutputStream(outFile);

        // Create a BufferedOutputStream with buffer array size of 16384 (16384 bytes = 16 KB).
        BufferedOutputStream br = new BufferedOutputStream(os, 16384);

        br.write("JP".getBytes("UTF-8"));
        br.write('\n');
        br.write("日本".getBytes("UTF-8"));
        br.write('\n');
        br.flush();

        br.write('\n');
        br.write("八洲".getBytes("UTF-8"));

        br.close();
    }
}
Output:
outfile.txt
JP
日本

八洲

Java IO Tutorials

Show More