Java Files newBufferedWriter() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.newBufferedWriter(path) opens a file for writing text and returns a BufferedWriter. Characters are collected in memory and written in large blocks, which makes many small writes fast.

The Java 8 overload encodes UTF-8; the Java 7 overload takes a Charset. By default it creates the file or truncates an existing one (CREATE, TRUNCATE_EXISTING, WRITE).

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.newBufferedWriter(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Open in try-with-resources:

Java
try (BufferedWriter out = Files.newBufferedWriter(path)) {
    out.write("first line");
    out.newLine();
}
CallWhat it does
Files.newBufferedWriter(p)UTF-8 writer, replaces content.
Files.newBufferedWriter(p, cs, APPEND, CREATE)Append, creating if needed.
out.newLine()Write the platform line separator.
out.flush()Push buffered text to the file now.

Since Java 7 (with charset) and Java 8 (UTF-8 default). Closing the writer flushes it automatically.

Library declarations
Java
public static BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException
public static BufferedWriter newBufferedWriter(Path path, Charset cs, OpenOption... options) throws IOException

Parameters

ParameterDescription
pathThe file to write.
cs(Java 7 overload) Charset for encoding.
optionsOpen options such as APPEND, CREATE, CREATE_NEW.

Return Value

Return ValueDescription
BufferedWriterAn open writer. Close it to flush and release the file.

Compare with Similar Methods

Text output:

MethodHow it differs
write()All lines in one call.
newBufferedReader()The reading counterpart.
newOutputStream()Bytes instead of characters.

Java Examples

Example 1 — Write a Small Report Line by Line

Use write and newLine() in a loop, then read the lines back.

Java
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class NewBufferedWriterReport {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path report = dir.resolve("report.txt");
        String[] names = {"Ana", "Ben", "Cy"};
        int[] scores = {91, 78, 85};
        try (BufferedWriter out = Files.newBufferedWriter(report)) {
            out.write("name,score");
            out.newLine();
            for (int i = 0; i < names.length; i++) {
                out.write(names[i] + "," + scores[i]);
                out.newLine();
            }
        }
        for (String line : Files.readAllLines(report)) {
            System.out.println(line);
        }
        Files.delete(report);
        Files.delete(dir);
    }
}

How It Works

  1. Header first. The column names are written before the loop, each line closed with newLine().
  2. Platform separator. newLine() writes \r\n on Windows and \n elsewhere, which is why the file is read back as lines rather than measured.
  3. Closed, then read. Leaving the try block flushes and closes the writer, so readAllLines sees all four lines.

Example 2 — Nothing Reaches the File Until flush()

Write five characters and look at the file size before and after flushing.

Java
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class NewBufferedWriterFlush {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path draft = dir.resolve("draft.txt");
        try (BufferedWriter out = Files.newBufferedWriter(draft)) {
            out.write("draft");
            System.out.println("Before flush: " + Files.size(draft) + " bytes");
            out.flush();
            System.out.println("After flush: " + Files.size(draft) + " bytes");
        }
        Files.delete(draft);
        Files.delete(dir);
    }
}

How It Works

  1. Created empty. Opening the writer creates draft.txt with size 0.
  2. Still in memory. "draft" sits in the writer's buffer (8192 characters by default), so the file is still 0 bytes.
  3. flush() writes it. After flushing, the five bytes are in the file. Closing would have flushed too.

Example 3 — Append One Entry per Run

Simulate three program runs that each append a line, using the Java 7 charset overload with CREATE and APPEND.

Java
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class NewBufferedWriterAppend {
    static void logRun(Path log, int run) throws IOException {
        try (BufferedWriter out = Files.newBufferedWriter(log, StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
            out.write("run " + run);
            out.newLine();
        }
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path log = dir.resolve("runs.log");
        for (int run = 1; run <= 3; run++) {
            logRun(log, run);
        }
        System.out.println(Files.readAllLines(log));
        Files.delete(log);
        Files.delete(dir);
    }
}

How It Works

  1. CREATE plus APPEND. The first run creates runs.log; later runs add to its end instead of truncating.
  2. Open briefly. Each call opens, writes one line, and closes, as a real program started three times would.
  3. Default would lose data. Without options, every run would apply TRUNCATE_EXISTING and only "run 3" would remain.

Frequently Asked Questions

Open try (BufferedWriter out = Files.newBufferedWriter(path)) and call write(text) and newLine() for each line.
Yes, by default it creates or truncates it. Pass CREATE and APPEND to add to the end.
UTF-8 for the Java 8 overload without a Charset; the Java 7 overload uses the charset you pass.
The text is still buffered. Call flush() or, better, close the writer with try-with-resources.
The platform line separator, System.lineSeparator(): \r\n on Windows and \n on Linux and macOS.

Next: Files.newByteChannel()

Open a file as a SeekableByteChannel to read and write at any position.

newByteChannel() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful