Skip to main content

BufferedOutputStream class in java

java.io.BufferedOutputStream is a class in the Java programming language that provides buffering capabilities to the OutputStream class, which is used for writing data to an output stream. The BufferedOutputStream class implements a buffer that can be used to improve performance when writing data to a file or network socket.

The BufferedOutputStream class maintains an internal buffer of bytes that can be written to an output stream. When data is written to the BufferedOutputStream, it is first written to the buffer, and when the buffer becomes full, the contents of the buffer are flushed to the underlying output stream.

The advantage of using BufferedOutputStream over OutputStream is that it reduces the number of write() calls made to the underlying stream, which can improve the performance of the application. BufferedOutputStream also provides additional methods for flushing the buffer and closing the stream.

Inside of this class exists 3 public methods. This class are providing the functionality to write text content into file. Declaration of this class as follows.

public class java.io.BufferedOutputStream 
extends java.io.FilterOutputStream

BufferedOutputStream public method

There are following useful methods which is define the inside of java.io.BufferedOutputStream class.

MethodDescription
void flush() It flushes this buffered output stream.
void write(int) It writes the specified byte to this buffered output stream.
void write(byte[], int, int) It writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Public Constructors

public java.io.BufferedOutputStream(java.io.OutputStream)
public java.io.BufferedOutputStream(java.io.OutputStream,int)
// Java program for java.io.BufferedOutputStream class flush() method
// Useful packages
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
class Example
{
	public static void main(String[] args)
	{
		// Create Instance
		try
		{
			// data.txt is file in current directory
			FileOutputStream f1 = new FileOutputStream("data1.txt");
			FileOutputStream f2 = new FileOutputStream("data2.txt");

			// Create an instance of BufferedOutputStream
			BufferedOutputStream x = new BufferedOutputStream(f1);
			BufferedOutputStream y = new BufferedOutputStream(f2,10);
			// Write value
			// Input ASCII value
			x.write(72); // H
			x.write(97); // a
			x.write(112); // p
			x.write(112); // p
			x.write(121); // y
			x.flush();
			// or
			y.write(' '); // 32
			y.write('?'); // 63
			y.flush(); 
	
		
			f1.close();
			x.close();
			f2.close();
			y.close();


			// data1.txt : Happy 
			// data2.txt :  ?

			System.out.println("Submit");
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
BufferedOutputStream class instance in java
Submit

java.io.BufferedOutputStream write(byte[], int, int) method example

// Java program for java.io.BufferedOutputStream class flush() method
// Useful packages
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
class Example
{
	public static void main(String[] args)
	{
		// Create Instance
		try
		{
			// data.txt is file in current directory
			FileOutputStream f = new FileOutputStream("data.txt");
			

			// Create an instance of BufferedOutputStream
			BufferedOutputStream x = new BufferedOutputStream(f,10);

			byte [] info = {65,90,97,101,105,65,66,76};
			// Write value
			x.write(info,2,4); 
			x.flush();

			x.close();
			f.close();
			System.out.println("Submit"); // aeiA
			
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
Submit




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment