Java Text File Write writeStringListToFile(File file, List lines)

Here you can find the source of writeStringListToFile(File file, List lines)

Description

Writes a string list to a file.

License

Open Source License

Parameter

Parameter Description
file is an existing file which can be written to.
lines a parameter

Exception

Parameter Description
IllegalArgumentException if param does not comply.
FileNotFoundException if the file does not exist.
IOException if problem encountered during write.

Declaration

static public void writeStringListToFile(File file, List<String> lines)
        throws FileNotFoundException, IOException 

Method Source Code


//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");
            }
        }
    }
}

Related

  1. writeStringField(String name, String value, JsonGenerator jgen)
  2. writeStringFile(String FileName, String text)
  3. writeStringInto(String s, File outputFile)
  4. writeStringInto(String s, File outputFile)
  5. writeStringListToFile(File file, ArrayList lines)
  6. writeStringTo(File outFile, String data)
  7. writeStringTo(String _value, File _target)
  8. writeStringToFile(@Nonnull String string, @Nonnull final File file)
  9. writeStringToFile(CharSequence contents, String filename)