Share on Google+Share on Google+

Java file BufferedWriter

This section demonstrates you the use of BufferedWriter class.

Java file BufferedWriter

This section demonstrates you the use of BufferedWriter class.

The BufferedWriter is a character stream class which allows to write the strings, arrays or characters data directly to the file. It provides buffering to the Writer. Using this class you can write a larger block at a time rather than write one character at a time to the network or disk. It is much faster, especially for disk access and larger data amounts.

In the given example, we have allowed the user to enter the data to be stored into the file. As the user writes the data on the console, InputStream reads the data from it. This data is then stored into the file using the method write() of BufferedWriter class.

read(): This method of InputStream class reads the next byte of data from the input stream.

write(int ch): This method of BufferedWriter class write a single character.

flush(): This method of BufferedWriter class flushes the stream.

Here is the code:


public class FileBufferedWriter {
	public static void main(String[] args) throws Exception {
		System.out.println("Enter text to store in the file:");
		InputStream in =;
		BufferedWriter bw = new BufferedWriter(new FileWriter("C:/output.txt"));
		int i;
		while ((i = != -1) {
			bw.write((char) i);



Posted on: May 2, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.