Java java.nio.file.Files.newBufferedWriter()
方法
Files.newBufferedWriter()
方法用于创建一个 BufferedWriter
对象,该对象可以高效地将文本写入指定文件。与传统的 FileWriter
相比,它提供了缓冲功能,能显著提高写入性能。
方法定义
public static BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException
public static BufferedWriter newBufferedWriter(Path path, Charset charset, OpenOption... options) throws IOException
public static BufferedWriter newBufferedWriter(Path path, Charset charset, OpenOption... options) throws IOException
参数解析
Path path
- 作用:指定要写入的文件路径
- 示例:
Path path = Paths.get("example.txt");
Charset charset
(可选)
- 作用:指定字符编码格式
- 默认值:如果不指定,默认使用 UTF-8 编码
- 常用编码:
StandardCharsets.UTF_8
StandardCharsets.ISO_8859_1
StandardCharsets.US_ASCII
OpenOption... options
(可选)
- 作用:指定文件打开方式
- 常用选项:
StandardOpenOption.CREATE
- 如果文件不存在则创建StandardOpenOption.APPEND
- 追加到文件末尾StandardOpenOption.TRUNCATE_EXISTING
- 清空已有文件内容StandardOpenOption.WRITE
- 以写入方式打开
基本用法示例
示例 1:基本写入
实例
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(); // 写入换行符
writer.write("This is a new line.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
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(); // 写入换行符
writer.write("This is a new line.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例 2:指定编码和选项
实例
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();
}
}
}
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();
}
}
}
高级用法
批量写入大量数据
实例
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();
}
}
}
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();
}
}
}
最佳实践
- 使用 try-with-resources:确保
BufferedWriter
在使用后自动关闭 - 处理异常:始终处理可能出现的
IOException
- 合理选择缓冲区大小:默认缓冲区大小通常足够,但可以通过
BufferedWriter
构造函数调整 - 考虑编码:明确指定字符编码以避免跨平台问题
- 批量写入:对于大量数据,考虑批量写入而非单次写入
常见问题解答
Q1: 与 FileWriter
相比有什么优势?
newBufferedWriter()
提供了缓冲功能,减少了实际的 I/O 操作次数,显著提高了写入性能。
Q2: 如何确保文件被正确关闭?
使用 try-with-resources 语法可以确保资源被自动关闭,即使在发生异常的情况下。
Q3: 追加模式和覆盖模式有什么区别?
- 追加模式 (
APPEND
) 会在文件末尾添加新内容 - 覆盖模式 (
TRUNCATE_EXISTING
) 会清空文件原有内容
总结
Files.newBufferedWriter()
是 Java NIO 中一个强大而灵活的文件写入工具,它结合了缓冲技术和多种打开选项,为文件写入操作提供了高效且安全的解决方案。通过合理使用这个方法,可以显著提高文件写入的性能和可靠性。
点我分享笔记