Java FileWriter Write saveToFile(List lines, String pathname)

Here you can find the source of saveToFile(List lines, String pathname)

Description

save To File

License

Open Source License

Declaration

public static void saveToFile(List<String> lines, String pathname) throws IOException 

Method Source Code


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

Related

  1. saveTextFile(String contents, File file)
  2. saveTextFile(String fileName, String text)
  3. saveToCache(String md, String in, String res)
  4. saveToFile(File file, String string)
  5. saveToFile(File file, String text)
  6. saveToFile(List list, String filename, boolean append)
  7. saveToFile(String fileName, String resultStr)
  8. saveToFile(String fileName, String str)
  9. saveToFile(String location, File tempFile)