Buffered Writer Example Java Program

Definition

The Buffered Writer writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Buffered Writer Example Program

import java.io.*;

public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
		StringWriter strwrit = null;
		BufferedWriter buffwrit = null;
		String str = "She sells sea shells on the sea shore.";
		try{
			strwrit = new StringWriter();
			buffwrit = new BufferedWriter(strwrit);
			buffwrit.write(str);
			buffwrit.flush();
			StringBuffer strbuff = strwrit.getBuffer();
			System.out.println(strbuff);
		}catch(IOException e){
			e.printStackTrace();
			}finally{
			if(strwrit!=null)
            strwrit.close();
			if(buffwrit!=null)
            buffwrit.close();
		}
	}
}

Sample Output

Output is:
She sells sea shells on the sea shore.