Java Write String to File writeFile(File file, String text)

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

Description

write File

License

Apache License

Declaration

public static void writeFile(File file, String text) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.io.Writer;

public class Main {
    private static final String ENCODING = "UTF-8";

    public static void writeFile(File file, String text) throws Exception {
        writeFile(file, text, false);/*from   www .j a  v  a  2 s  . c  o  m*/
    }

    public static void writeFile(File file, String text, boolean append)
            throws Exception {
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file), ENCODING));
            out.append(text);
            out.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. writeFile(File file, String data)
  2. writeFile(File file, String data)
  3. writeFile(File file, String data, boolean append)
  4. writeFile(File file, String dataString)
  5. writeFile(File file, String text)
  6. writeFile(File file, String... lines)
  7. writeFile(File file, String... lines)
  8. writeFile(File file, StringBuffer buffer)
  9. writeFile(File filename, ArrayList lines)