BufferedOutputStream class


java.lang.Object
 |
 +-java.io.OutputStream
    |
    +-java.io.FilterOutputStream
       |
       +java.io.BufferedOutputStream

The class implements a buffered output stream.

A BufferedOutputStream adds flush( ) method to ensure that data buffers are written to the output device.

You may need to call flush( ) to cause any data that is in the buffer to be immediately written. Buffers for output in Java are there to increase performance.

Here are the two available constructors:

BufferedOutputStream(OutputStream outputStream)
creates a buffered stream using the default buffer size.
BufferedOutputStream(OutputStream outputStream, int bufSize)
the size of the buffer is passed in bufSize.
void flush()
Flushes this buffered output stream.
void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
void write(int b)
Writes the specified byte to this buffered output stream.

Revised from Open JDK source code


import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(
        "yourFile.txt"));
    bufferedOutput.write("java2s.com".getBytes());
    bufferedOutput.write("\n".getBytes());
    bufferedOutput.write('a');

    bufferedOutput.write(65);
    bufferedOutput.close();
  }
}
Home 
  Java Book 
    File Stream