Java BufferedWriter
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java BufferedWriter

Harine

Java BufferedWriter

Writing Text Efficiently with BufferedWriter

When working with text files in Java, performance and efficiency become important—especially when writing large amounts of data. The BufferedWriter class is designed to improve file writing performance by using an internal memory buffer.

Instead of writing each character directly to the disk, BufferedWriter temporarily stores data in memory and writes it in larger blocks. This reduces disk access operations and makes file writing significantly faster.

BufferedWriter is typically used together with FileWriter, which handles the creation or opening of the file.

Writing Text to a File

You can write text to a file using the write() method. To add line breaks between lines of text, the newLine() method can be used.

The following example demonstrates how to write multiple lines to a file.

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

public class BufferedWriterExample {

  public static void main(String[] args) {

    try (BufferedWriter writer =

         new BufferedWriter(new FileWriter("journal.txt"))) {

     writer.write("Daily Programming Notes");

     writer.newLine();

     writer.write("Practiced Java file handling concepts.");

     writer.newLine();

     writer.write("Buffered streams improve I/O performance.");

     System.out.println("File written successfully.");

   } catch (IOException error) {

     System.out.println("Unable to write to the file.");

     error.printStackTrace();

     }

   }

 }

Sample Output

File written successfully.

Explanation

  1. The program opens or creates a file named journal.txt using FileWriter.
  2. BufferedWriter wraps the FileWriter to improve writing performance.
  3. The write() method inserts text into the file.
  4. The newLine() method adds a platform-independent line break.
  5. The try-with-resources statement automatically closes the writer after use.

If the file already exists, its contents will be overwritten.

Appending Text to an Existing File

By default, BufferedWriter overwrites the existing content of a file. To preserve the existing data and add new content at the end, you must enable append mode.

This can be done by passing true as the second argument to the FileWriter constructor.

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

public class AppendWriterExample {

   public static void main(String[] args) {

     try (BufferedWriter writer =

         new BufferedWriter(new FileWriter("journal.txt", true))) {

       writer.newLine();

       writer.write("New entry: Practiced buffered file writing.");

       System.out.println("Text appended successfully.");

    } catch (IOException error) {

       System.out.println("Failed to append data.");

       error.printStackTrace();

      }

    }

  }

Sample Output

Text appended successfully.

Explanation

In this example:

  • The file journal.txt is opened in append mode.
  • The existing content remains unchanged.
  • The new text is added at the end of the file.

Our website uses cookies to enhance your experience. Learn More
Accept !