Java Text File Save writeFile(String string, File location, boolean forceASCII)

Here you can find the source of writeFile(String string, File location, boolean forceASCII)

Description

write File

License

Open Source License

Declaration

public static boolean writeFile(String string, File location, boolean forceASCII) 

Method Source Code

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

import java.io.*;

public class Main {
    public static boolean writeFile(String string, File location) {
        return writeFile(string, location, false);
    }/*  ww  w .j  a  va  2  s.  c o m*/

    public static boolean writeFile(String string, File location, boolean forceASCII) {
        if (!location.exists())
            location.getParentFile().mkdirs();
        try {
            if (location.exists())
                location.delete();
            location.createNewFile();

            if (!forceASCII) {
                FileWriter fstream = new FileWriter(location);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(string);
                out.close();
            } else {
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(location), "ASCII"));
                try {
                    out.write(string);
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                } finally {
                    out.close();
                }
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            return false;
        }
        return true;
    }

    public static boolean delete(File resource) {
        if (resource.isDirectory()) {
            File[] childFiles = resource.listFiles();
            for (File child : childFiles) {
                delete(child);
            }
        }
        return resource.delete();
    }
}

Related

  1. writeFile(String sFileName, String sContent)
  2. writeFile(String sName, String data, String encoding)
  3. writeFile(String str, File f)
  4. writeFile(String str, String filename, boolean append)
  5. writeFile(String string, File file)
  6. writeFile(String tailored, File f)
  7. writeFile(String targetPath, String filename, byte[] content)
  8. writeFile(String text, File file, boolean append)
  9. writeFile(String text, File outf)