Here you can find the source of writeStringListToFile(File file, List
Parameter | Description |
---|---|
file | is an existing file which can be written to. |
lines | a parameter |
Parameter | Description |
---|---|
IllegalArgumentException | if param does not comply. |
FileNotFoundException | if the file does not exist. |
IOException | if problem encountered during write. |
static public void writeStringListToFile(File file, List<String> lines) throws FileNotFoundException, IOException
//package com.java2s; import java.io.*; import java.util.*; public class Main { /**/* w ww . jav a2s . c o m*/ * Writes a string list to a file. Existing files will be completely * replaced. * * @param file is an existing file which can be written to. * @param lines * @throws IllegalArgumentException if param does not comply. * @throws FileNotFoundException if the file does not exist. * @throws IOException if problem encountered during write. */ static public void writeStringListToFile(File file, List<String> lines) throws FileNotFoundException, IOException { if (file == null) { throw new IllegalArgumentException("File should not be null."); } // if (!file.exists()) { // throw new FileNotFoundException("File does not exist: " + file); // } // if (!file.isFile()) { // throw new IllegalArgumentException("Should not be a directory: " + file); // } // if (!file.canWrite()) { // throw new IllegalArgumentException("File cannot be written: " + file); // } try (BufferedWriter output = new BufferedWriter(new FileWriter(file))) { //FileWriter always assumes default encoding is OK! for (String line : lines) { output.write(line + "\n"); } } } }