Java File Write nio writeFile(File file, String content)

Here you can find the source of writeFile(File file, String content)

Description

write File

License

Open Source License

Declaration

public static void writeFile(File file, String content) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.nio.charset.StandardCharsets;

public class Main {
    public static void writeFile(File file, String content) throws IOException {
        writeFile(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), content);
    }//from   w w  w  . j a  v  a2 s.co m

    private static void writeFile(Writer fw, String content) throws IOException {
        try {
            fw.write(content);
            fw.close();
        } catch (IOException e) {
            closeSilently(fw);
            throw e;
        }
    }

    public static void closeSilently(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // Do nothing
            }
        }
    }
}

Related

  1. writeFile(File file, String content)
  2. writeFile(File file, String text)
  3. writeFile(FileObject fileObject, String content)
  4. writeFile(List content, String targetFile)