通过以下方式可以写文件
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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
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);
First of all, it is better to write the code above like this (see):
As for appending, just use the APPEND option of StandardOpenOption: