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();
}
Call
What 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
Parameter
Description
path
The file to write.
cs
(Java 7 overload) Charset for encoding.
options
Open options such as APPEND, CREATE, CREATE_NEW.
Return Value
Return Value
Description
BufferedWriter
An open writer. Close it to flush and release the file.
Run with java NewBufferedWriterFlush. Typical output; buffer sizes are a JDK implementation detail, but a 5-character write is buffered on Windows, macOS, and Linux alike.
Before flush: 0 bytes
After flush: 5 bytes
How It Works
Created empty. Opening the writer creates draft.txt with size 0.
Still in memory. "draft" sits in the writer's buffer (8192 characters by default), so the file is still 0 bytes.
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);
}
}
Output
Run with java NewBufferedWriterAppend. The output is the same on Windows, macOS, and Linux.
[run 1, run 2, run 3]
How It Works
CREATE plus APPEND. The first run creates runs.log; later runs add to its end instead of truncating.
Open briefly. Each call opens, writes one line, and closes, as a real program started three times would.
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.