Java Code Example for BufferedOutputStream : flush() Method

flush() method of BufferedOutStream flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.

Overrides: flush in class FilterOutputStream
Throws: IOException – if an I/O error occurs.

Below code example show how the output stream is flushed and writes out the output stream.


/**********************************************************************************
 * Created on Nov, 2004 Copyright(c) https://kodehelp.com All Rights Reserved. 
 **********************************************************************************/
package com.kodehelp.java.io.BufferedOutputStream;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author https://kodehelp.com
 *
 */
public class BufferedOutputStreamFlush {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedOutputStream bos =null;
		try{
			// Create object of FileOutputStream class.
	        FileOutputStream fos = new FileOutputStream("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
			bos = new BufferedOutputStream(fos);
			
			bos.flush();
			System.out.print("flush() method is called that forces buffered out "+
		      "bytes \n to be written out to the underlying output stream.");
			
		}catch(IOException ioe){
			System.out.println("Error while writing to System OutStream" + ioe);
		}finally{
			try
			{
				if(bos != null)
				{
					bos.flush();
					bos.close();
				}
 
			}catch(Exception e){
				e.printStackTrace();
			}
		}

	}

}