English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Java other topics

Java BufferedOutputStream class

In this tutorial, we will learn about Java BufferedOutputStream and its methods with the help of examples.

The BufferedOutputStream class in the java.io package is used together with other output streams to write data more efficiently (in bytes).

It inherits the OutputStream abstract class.

How BufferedOutputStream works

BufferedOutputStream maintains a8192Bytesinternal buffer.

During the write operation, bytes are written to the internal buffer instead of the disk. Once the buffer is filled or the stream is closed, the entire buffer will be written to the disk.

This reduces the number of communications with the disk. This is why writing bytes using BufferedOutputStream is faster.

Create a BufferedOutputStream

To create a BufferedOutputStream, we must first import the java.io.BufferedOutputStream package. After importing the package, we can create the output stream.

//Create a FileOutputStream
FileOutputStream file = new FileOutputStream(String path);
//Create a BufferedOutputStream
BufferedOutputStream buffer = new BufferedOutputStream(file);

In the above example, we created a BufferedOutputStream named buffer and used a FileOutputStream named file.

Here, the default size of the internal buffer is8192Bytes. However, we can also specify the size of the internal buffer.

//Create a BufferedOutputStream with a specified internal buffer size
BufferedOutputStream buffer = new BufferedOutputStream(file, int size);

This buffer will help to write bytes to the file faster.

Methods of BufferedOutputStream

The BufferedOutputStream class provides implementations for different methods in the OutputStream class.

write() method

  • write() - Write a single byte to the internal buffer of the output stream

  • write(byte[] array) - Write the bytes in the specified array to the output stream

  • write(byte[] arr, int start, int length)- Write the specified number of bytes equal to length from the start position to the output stream of the array

Example: BufferedOutputStream writes data to a file

import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
public class Main {
    public static void main(String[] args) {
        String data = "This is a line of text inside the file";
        try {
            //Create a FileOutputStream
            FileOutputStream file = new FileOutputStream("output.txt");
            //Create a BufferedOutputStream
            BufferedOutputStream output = new BufferedOutputStream(file);
            byte[] array = data.getBytes();
            //Write data to the output stream
            output.write(array);
            output.close();
        }
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

In the above example, we created a buffered output stream named output and a FileOutputStream. The output stream is linked to the file output.txt.

FileOutputStream file = new FileOutputStream("output.txt");
BufferedOutputStream output = new BufferedOutputStream(file);

To write data to the file, we used the write() method.

Here, when we run the program,output.txtThe file will write the following content.

This is a line of text inside the file.

Note:The getBytes() method is used in the program to convert a string to a byte array.

flush() method

To clear the internal buffer, we can use the flush() method. This method forces the output stream to write all the data in the buffer to the target file. For example,

import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
public class Main {
    public static void main(String[] args) {
        String data = "This is a demo of the flush method";
        try {
            //Create a FileOutputStream
            FileOutputStream file = new FileOutputStream("flush.txt");
            //Create a BufferedOutputStream
            BufferedOutputStream buffer = new BufferedOutputStream(file);
            //Write data to the output stream
            buffer.write(data.getBytes());
            //Push data into the target
            buffer.flush();
            System.out.println("Data is pushed into the file.");
            buffer.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

Output Result

Data is pushed into the file.

When we run the program, the file flush.txt is filled with text represented by string data.

close() Method

To close the buffered output stream, we can use the close() method. After calling this method, we will not be able to use the output stream to write data.