import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; import org.apache.commons.io.FileUtils; public class WriteListToFile { public static void main(String[] args) throws IOException { Listnames = Arrays.asList("Alice", "Bob", "Charlie", "David"); File file = new File("names.txt"); FileUtils.writeLines(file, names); } }
import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import org.apache.commons.io.FileUtils; public class WriteListToFileEncoding { public static void main(String[] args) throws IOException { ListThis example is similar to the first, but it also specifies the character encoding to be used when writing to the file. In this case, UTF-8 is used.names = Arrays.asList("Alice", "Bob", "Charlie", "David"); File file = new File("names.txt"); FileUtils.writeLines(file, StandardCharsets.UTF_8.name(), names); } }