Open In App

Java.io.BufferedWriter class methods in Java

Last Updated : 30 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The BufferedWriter class in Java is used to write text to character-based output streams efficiently by buffering characters. It reduces the number of I/O operations by writing large chunks at once instead of sending each character individually. It also provides convenient methods like write(), newLine(), and flush().

Class Declaration 

public class BufferedWriter extends Writer

Constructors write

  • BufferedWriter(Writer out): Creates a buffered character-output stream that uses a default-sized output buffer.
  • BufferedWriter(Writer out, int size): Creates a new buffered character-output stream that uses an output buffer of the given size.

Methods in BufferedWriter class

MethodAction
close()Closes the stream and releases system resources. Flushes the buffer before closing.
flush()Forces any buffered characters to be written to the output stream.
newLine()Writes a platform-dependent line separator.
write(int c)Writes a single character.
write(char[] cbuf, int off, int len)Writes a portion of a character array.
write(String s, int off, int len)Writes a portion of a string.

Implemention of some common methods

1. close()

Closes the writer and releases system resources. It also flushes the buffer before closing. After closing, no further write operations can be performed.

Syntax:

public void close() throws IOException

Implementation:

Java
import java.io.*;

public class CloseExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        bw.write("Hello World!");
        bw.close(); // flushes and closes the stream
        System.out.println("Stream closed");
    }
}

Output
Stream closed

2. flush()

Forces all buffered data to be written immediately to the underlying stream. Useful when you want to ensure data is written without closing the stream.

Syntax:

public void flush() throws IOException

Implementation:

Java
import java.io.*;

public class FlushExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        bw.write("Flushing Example");
        bw.flush(); // ensures content is written
        System.out.println("Content was written and stream is flushed");
        bw.close();
    }
}

Output
Content was written and stream is flushed

3. newLine()

Writes a platform-dependent line separator (\n in Linux/Mac, \r\n in Windows).

Syntax:

public void newLine() throws IOException

Example:

Java
import java.io.*;

public class NewLineExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        bw.write("First Line");
        bw.newLine(); // adds line break
        bw.write("Second Line");
        bw.close();
    }
}

4. write(int c)

Writes a single character to the output stream.

Syntax:

public void write(int c) throws IOException

Implementation:

Java
import java.io.*;

public class WriteCharExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        bw.write(65); // writes 'A' (ASCII 65)
        bw.close();
    }
}

5. write(char[] cbuf, int off, int len)

Writes a portion of a character array to the stream.

Syntax:

public void write(char[] cbuf, int off, int len) throws IOException

Implementation:

Java
import java.io.*;

public class WriteCharArrayExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        char[] arr = { 'J', 'a', 'v', 'a', '!', '!' };
        bw.write(arr, 0, 4); // writes "Java"
        bw.close();
    }
}

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

Writes a portion of a string starting at the given offset for the specified length.

Syntax:

public void write(String s, int off, int len) throws IOException

Implementation:

Java
import java.io.*;

public class WriteStringExample {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        String str = "BufferedWriter Example";
        bw.write(str, 0, 9); // writes "BufferedW"
        bw.close();
    }
}

Article Tags :
Practice Tags :

Explore