Java BufferedOutputStream Class Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report BufferedOutputStream class in Java is a part of the java.io package. It improves the efficiency of writing data to an output stream by buffering the data. This reduces the number of direct writes to the underlying output stream, making the process faster and more efficient.Example 1: The below Java program demonstrates how to use the bufferedOutputStream class to efficiently write data to a file by buffering the output. Java // Writing data to a file // using BufferedOutputStream import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class Geeks { public static void main(String[] args) { String s = "This is an example of BufferedOutputStream."; try (FileOutputStream fileOutputStream = new FileOutputStream("output.txt"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) { // Convert string to bytes and write to the // buffered output stream bufferedOutputStream.write(s.getBytes()); System.out.println( "Data written to the file successfully."); } catch (IOException e) { e.printStackTrace(); } } } OutputData written to the file successfully. Declaration of BufferedOutputStream Classpublic class BufferedOutputStream extends FilterOutputStreamConstructor of BufferedOutputStream classBufferedOutputStream(OutputStream out): Creates a buffered output stream with a default buffer size of 8192 bytes.BufferedOutputStream(OutputStream out, int size): Creates a buffered output stream with the specified buffer size.Methods of BufferedOutputStream ClassMethodAction performedwrite(int b)Writes the specified byte to this buffered output streamwrite(byte[] b, int off, int len)Writes len bytes from the specified byte array starting at offset off to this buffered output stream. flush()Forces any buffered output bytes to be written outExample 2: The below Java program demonstrates how to use BufferedOutputStream class to write data to a file effectively by buffering output using methods like write(), flush() and close() Java // Java program demonstrating BufferedOutputStream import java.io.*; class Geeks { public static void main(String args[]) throws Exception { FileOutputStream fout = new FileOutputStream("f1.txt"); // creating bufferdOutputStream obj BufferedOutputStream bout = new BufferedOutputStream(fout); // illustrating write() method for (int i = 65; i < 75; i++) { bout.write(i); } byte b[] = { 75, 76, 77, 78, 79, 80 }; bout.write(b); // illustrating flush() method bout.flush(); // illustrating close() method bout.close(); fout.close(); } } Output: Note: A file named f1.txt is created in the current working directory and the file will contain ABCDEFGHIJKLMNOP. Comment More infoAdvertise with us Next Article Java BufferedOutputStream Class N Nishant Sharma Improve Article Tags : Java Java-Classes Java-IO package Practice Tags : Java Similar Reads Java.io.BufferedInputStream class in Java A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refil 4 min read FileOutputStream in Java In Java, the FileOutputStream class is a subclass of OutputStream. It is used to write data to a file as a stream of bytes. FileOutputStream is commonly employed for writing primitive values into a file. FileOutputStream supports writing both byte-oriented and character-oriented data.Note: FileWrite 7 min read Java.io.ByteArrayOutputStream() Class in Java java.io.ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class. Thu 4 min read BufferedOutputStream write() method in Java with Examples The write(int) method of BufferedOutputStream class in Java is used to write the specified byte to the buffered output stream. The specified byte is passed as an integer to the write() method here. It is used to write one byte as a time to the BufferedOutputStream. Syntax: public void write(int b) t 3 min read BufferedInputStream close() method in Java with Examples The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch 2 min read Like