java.io.BufferedOutputStream Class

A BufferedOutputStream is similar to any OutputStream with the exception of an added flush() method that is used to ensure that data buffers are written to the stream being buffered. Since the point of a BufferedOutputStream is to improve performance by reducing the number of times the system actually writes data, you may need to call flush() to cause any data that is in the buffer to be immediately written.

Unlike buffered input, buffering output does not provide additional functionality. Buffers for output in Java are there to increase performance.

java.io.BufferedOutputStream class Example
/* Following example shows how to read the text from file using java BufferedOutputStream. */
/* Save with file name BufferedOutputStreamExample.java */
import java.io.*;
public class BufferedOutputStreamExample
{
	public static void main(String args[])
	{
		//BufferedOutputStream DECLARATION
		BufferedOutputStream bos = null;
		//FileOutputStream DECLARATION
		FileOutputStream fos = null;
		try
		{
			fos = new FileOutputStream("alphabets.txt");
			bos = new BufferedOutputStream(fos);
			//WRITES THE BYTES TO BufferedOutputStream
			for(int i=0;i<26;i++)
			{
				bos.write((byte) ('A'+i));
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(bos!=null)
					bos.close();
				if(fos!=null)
					fos.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}
                        

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

java.io.BufferedOutputStream class Example 2
/* Following example shows how to read total bytes from file into array */
/* and writes to file using java BufferedOutputStream. */
/* Save with file name BufferedOutputStreamExample2.java */
import java.io.*;
public class BufferedOutputStreamExample2
{
	public static void main(String args[])
	{
		// BufferedInputStream DECLARATION
		BufferedInputStream bis = null;
		//FileInputStream DECLARATION
		FileInputStream fis = null;
		//ARRAY TO READ BYTES
		byte bytebuff[] = null;
		//BufferedOutputStream DECLARATION
		BufferedOutputStream bos = null;
		//FileOutputStream DECLARATION
		FileOutputStream fos = null;
		try
		{
			fis = new FileInputStream("BufferedOutputStreamExample2.java");
			bis = new BufferedInputStream(fis);
			//GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
			bytebuff = new byte[fis.available()];
			//READ TOTAL BYTES FROM FILE
			bis.read(bytebuff, 0,fis.available());
			fos = new FileOutputStream("duplicatefile.txt");
			bos = new BufferedOutputStream(fos);
			//WRITES THE BYTES TO BufferedOutputStream
			bos.write(bytebuff,0,bytebuff.length);
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				//RELEASE ARRAY MEMORY
				bytebuff = null;
				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
				if(bos!=null)
					bos.close();
				if(fos!=null)
					fos.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}