Java java.nio.file.Files.newBufferedWriter() Method
Files.newBufferedWriter() method is used to create a BufferedWriter object that can efficiently write text to a specified file. Compared with traditional FileWriter , it provides buffering, which can significantly improve write performance.
Method Definition
public
static
BufferedWriter
newBufferedWriter
(
Path path, OpenOption...
options
)
throws
IOException
public
static
BufferedWriter
newBufferedWriter
(
Path path, Charset charset, OpenOption...
options
)
throws
IOException
Parameter analysis
Path path
- Purpose: Specifies the file path to write to
- Example:
Path path = Paths.get("example.txt");
Charset charset (optional)
- Purpose: Specifies the character encoding format
- Default value: If not specified, UTF-8 encoding is used by default
- Common encodings:
StandardCharsets.UTF_8StandardCharsets.ISO_8859_1StandardCharsets.US_ASCII
OpenOption... options (optional)
- Purpose: Specifies how the file is opened
- Common options:
StandardOpenOption.CREATE- Create if the file does not existStandardOpenOption.APPEND- Append to the end of the fileStandardOpenOption.TRUNCATE_EXISTING- Truncate existing file contentStandardOpenOption.WRITE- Open for writing
Basic Usage Example
Example 1: Basic writing
Example
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardOpenOption
;
public
class
BufferedWriterExample
{
public
static
void
main
(
String
[
]
args
)
{
Path path
=
Paths.
get
(
"output.txt"
)
;
try
(
BufferedWriter
writer
=
Files.
newBufferedWriter
(
path
)
)
{
writer.
write
(
"Hello, World!"
)
;
writer.
newLine
(
)
;
// Write a newline
writer.
write
(
"This is a new line."
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Example 2: Specify encoding and options
Example
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.Paths
;
import
java.nio.file.StandardOpenOption
;
public
class
BufferedWriterExample2
{
public
static
void
main
(
String
[
]
args
)
{
Path path
=
Paths.
get
(
"output_append.txt"
)
;
try
(
BufferedWriter
writer
=
Files.
newBufferedWriter
(
path,
StandardCharsets.
UTF_8
,
StandardOpenOption.
CREATE
,
StandardOpenOption.
APPEND
)
)
{
writer.
write
(
"Appended line 1"
)
;
writer.
newLine
(
)
;
writer.
write
(
"Appended line 2"
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Advanced usage
Batch write large amounts of data
Example
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.stream.IntStream
;
public
class
LargeFileWriter
{
public
static
void
main
(
String
[
]
args
)
{
Path path
=
Paths.
get
(
"large_file.txt"
)
;
try
(
BufferedWriter
writer
=
Files.
newBufferedWriter
(
path
)
)
{
IntStream.
range
(
0
,
100000
)
.
forEach
(
i
->
{
try
{
writer.
write
(
"Line "
i
"\n"
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Best Practices
- Using try-with-resources: Ensure that
BufferedWriterAutomatically closed after use - Handle Exceptions: Always handle possible
IOException - Choose the buffer size reasonably: The default buffer size is usually sufficient, but can be adjusted via the
BufferedWriterconstructor - Consider encoding: Explicitly specify character encoding to avoid cross-platform issues
- Batch writing: For large amounts of data, consider batch writing rather than single writes
Frequently Asked Questions
Q1: Compared with FileWriter , what are the advantages?
newBufferedWriter() provides buffering, reducing the number of actual I/O operations and significantly improving write performance.
Q2: How to ensure the file is closed correctly?
Using the try-with-resources syntax ensures resources are automatically closed, even when an exception occurs.
Q3: What is the difference between append mode and overwrite mode?
- Append mode (
APPEND) adds new content at the end of the file - Overwrite mode (
TRUNCATE_EXISTING) clears the original file content
Summary
Files.newBufferedWriter() is a powerful and flexible file writing tool in Java NIO. It combines buffering technology and multiple open options to provide an efficient and safe solution for file writing operations. Used properly, it can significantly improve the performance and reliability of file writing.
Java java.nio.file.Files