Java:使用NIO2写文件时如何追加文件内容呢?
高洛峰
高洛峰 2017-04-17 17:56:19
0
1
474

通过以下方式可以写文件

List<String> list = new ArrayList<String>();
list.add("第一行");
list.add("第二行");
list.add("第三行");
 
Path path = Paths.get("E:" + File.separator + "demo.txt");
Files.write(path, list, Charset.forName("UTF-8"));   

但如果想每次执行都追加内容该怎么办呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
黄舟

First of all, it is better to write the code above like this (see):

Path path = Paths.get("E:", "demo.txt");
Files.write(path, list, StandardCharsets.UTF_8);

As for appending, just use the APPEND option of StandardOpenOption:

Files.write(path, list, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!