In this tutorial, we will learn about Java BufferedOutputStream and its techniques with the help of examples.
The BufferedOutputStream class of the java.io package is used with other output streams to write the data (in bytes) all the more effectively.
It extends the OutputStream abstract class.
In this article, you will learn-
Working of BufferedOutputStream
The BufferedOutputStream keeps up an inside buffer of 8192 bytes.
During the writing activity, the bytes are kept in touch with the inward buffer rather than the disk. When the buffer is filled or the stream is closed, the entire buffer is kept in touch with the disk.
Subsequently, the quantity of correspondence to the disk is diminished. This is the reason writing bytes is quicker using BufferedOutputStream.
Make a BufferedOutputStream
To make a BufferedOutputStream, we should import the java.io.BufferedOutputStream package first. When we import the package here is the means by which we can make the output stream.
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream(String path);
// Creates a BufferedOutputStream
BufferedOutputStream buffer = new BufferOutputStream(file);
In the above example, we have made a BufferdOutputStream named buffer with the FileOutputStream named file.
Here, the inside buffer has the default size of 8192 bytes. Be that as it may, we can determine the size of the inside buffer too.
// Creates a BufferedOutputStream with specified size internal buffer
BufferedOutputStream buffer = new BufferOutputStream(file, int size);
The buffer will help to write bytes to files more quickly.
Methods of BufferedOutputStream
The BufferedOutputStream class provides implementations for different methods in the OutputStream class.
write() Method
- write() – writes a single byte to the internal buffer of the output stream
- write(byte[] array) – writes the bytes from the specified array to the output stream
- write(byte[] arr, int start, int length) – write the number of bytes equal to length to the output stream from an array starting from the position start
Example: BufferedOutputStream to write 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 {
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("output.txt");
// Creates a BufferedOutputStream
BufferedOutputStream output = new BufferedOutputStream(file);
byte[] array = data.getBytes();
// Writes data to the output stream
output.write(array);
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have made a buffered output stream named output alongside FileOutputStream. The output stream is connected with the record output.txt.
FileOutputStream file = new FileOutputStream("output.txt");
BufferedOutputStream output = new BufferedOutputStream(file);
To write data to the file, we have used the write() method.
Here when we run the program, the output.txt file is filled with the following content.
This is a line of text inside the file.
Note: The getBytes() technique used in the program changes over a string into an array of bytes.
flush() Method
To clear the inward buffer, we can use the flush() strategy. This technique forces the output stream to write all data present in the buffer to the objective file. For instance,
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 {
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream(" flush.txt");
// Creates a BufferedOutputStream
BufferedOutputStream buffer = new BufferedOutputStream(file);
// Writes data to the output stream
buffer.write(data.getBytes());
// Flushes data to the destination
buffer.flush();
System.out.println("Data is flushed to the file.");
buffer.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data is flushed to the file.
When we run the program, the file flush.txt is filled with the text represented by the string data.
close() Method
To close the buffered output stream, we can use the close() technique. When the technique is called, we can’t use the output stream to write the data.
To learn more, visit Java BufferedOutputStream (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.