Here you can find the source of saveToFile(List
public static void saveToFile(List<String> lines, String pathname) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import java.util.StringJoiner; public class Main { public static void saveToFile(List<String> lines, String pathname) throws IOException { File file = new File(pathname); if (!file.isFile()) { if (!file.createNewFile()) { System.err.println("fail to create new file: " + file.getAbsolutePath()); return; }/*from w w w . j a v a2s . c o m*/ } StringJoiner joiner = new StringJoiner("\n"); lines.forEach(joiner::add); String content = joiner.toString(); FileWriter fw = new FileWriter(file, false); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.flush(); bw.close(); fw.close(); } }