Java - BufferedWriter Class



Introduction

The Java BufferedWriter class writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.Following are the important points about BufferedWriter −

  • The buffer size may be specified, or the default size may be used.

  • A Writer sends its output immediately to the underlying character or byte stream.

Class declaration

Following is the declaration for Java.io.BufferedWriter class −

 public class BufferedWriter extends Writer 

Field

Following are the fields for Java.io.BufferedWriter class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

BufferedWriter(Writer out)

This creates a buffered character-output stream that uses a default-sized output buffer.

2

BufferedWriter(Writer out, int sz)

This creates a new buffered character-output stream that uses an output buffer of the given size.

Class methods

Sr.No. Method & Description
1 void close()

This method closes the stream, flushing it first.

2 void flush()

This method flushes the stream.

3 void newLine()

This method writes a line separator.

4 void write(char[] cbuf, int off, int len)

This method writes a portion of an array of characters.

5 void write(int c)

This method writes a single character.

6 void write(String s, int off, int len)

This method writes a portion of a String.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Writer
  • Java.io.Object

Example - Writing to a File and Closing the Writer

The following example shows the usage of Java BufferedWriter close() method.

BufferedWriterDemo.java

 package com.tutorialspoint; import java.io. BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; String content = "Hello, World!\nThis is a BufferedWriter example."; // Initialize BufferedWriter with a FileWriter try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Write content to the file writer.write(content); System.out.println("Content written to file."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Content written to file. 

File Output

The content "Hello, World!\nThis is a BufferedWriter example." is written to example.txt, and the writer is automatically closed.

Explanation

  • A BufferedWriter is created to write to a file (example.txt) using a FileWriter.

  • The try-with-resources statement is used to ensure that the BufferedWriter is automatically closed after writing.

  • The close() method is implicitly called when the try block exits, flushing any buffered data to the file and releasing resources.

  • This ensures that the content ("Hello, World!\nThis is a BufferedWriter example.") is properly saved to the file.

Example - Flushing Data Periodically in a Long-Running Process

The following example shows the usage of Java BufferedWriter flush() method.

BufferedWriterDemo.java

 package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Simulate writing data in a loop for (int i = 1; i <= 5; i++) { writer.write("Line " + i + ": This is a periodic flush example.\n"); // Flush the buffer after writing each line writer.flush(); System.out.println("Flushed data after writing line " + i); } } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Flushed data after writing line 1 Flushed data after writing line 2 Flushed data after writing line 3 Flushed data after writing line 4 Flushed data after writing line 5 

File Output

Following will be the content of example.txt file

 Line 1: This is a periodic flush example. Line 2: This is a periodic flush example. Line 3: This is a periodic flush example. Line 4: This is a periodic flush example. Line 5: This is a periodic flush example. 

Explanation

  • A BufferedWriter is created to write data to a file (example.txt).

  • Inside a loop, data is written line by line, simulating a scenario where data is generated or logged incrementally.

  • After writing each line, the flush() method is called to ensure that the data is immediately written to the file, even before the loop completes.

  • This approach is useful in scenarios such as logging real-time events or writing large data incrementally.

Example - Writing Dynamic Content Line by Line

The following example shows the usage of Java BufferedWriter newLine() method.

BufferedWriterDemo.java

 package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Simulate dynamic content for (int i = 1; i <= 5; i++) { writer.write("This is line number " + i); writer.newLine(); // Write a line separator after each line } System.out.println("Dynamic content written to the file successfully."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Dynamic content written to the file successfully. 

File Output

Following will be the content of example.txt file

 This is line number 1 This is line number 2 This is line number 3 This is line number 4 This is line number 5 

Explanation

  • A loop is used to simulate dynamically generated content.

  • For each iteration, a line of text is written to the file (example.txt), followed by a platform-independent line separator using newLine().

  • This ensures that each line appears on a separate line in the output file, regardless of the system's line separator.

Advertisements