Java BufferedWriter

Learn to write the content into a file in Java using BufferedWriter. Use the given below example as a template and reuse it based on the application requirements.

1. BufferedWriter class

  • BufferedWriter is a sub class of class.
  • BufferedWriter writes text to character based output stream. It uses buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
  • BufferedWriter is used to make lower-level classes like FileWriter more efficient and easier to use.
  • BufferedWriter uses relatively large chunks of data at once, thus minimizing the number of write operations for better performance.

Syntax for creating BufferedWriter

As said earlier, wrap the FileWriter instance into a BufferedWriter object.

BufferedWriter bw = 
	new BufferedWriter(new FileWriter("file.txt")));

2. BufferedWriter with FileWriter

The FileWriter class is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Example: Java Program to write a string to a File using BufferedWriter and FileWriter


public class BufferedWriterExample 
	public static void main(String[] args)
		try {
			String content = "Hello Learner !! Welcome to";
			File file = new File("c:/temp/samplefile.txt");

			if (!file.exists()) {
			FileWriter x` = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
		catch (IOException e) 

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.