Java Text File Save writeFile(String str, File f)

Here you can find the source of writeFile(String str, File f)

Description

write File

License

Open Source License

Declaration

public static void writeFile(String str, File f) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.Reader;

public class Main {
    public static void writeFile(String str, File f) throws IOException {
        if (!f.exists()) {
            if (!f.getParentFile().exists() && !f.getParentFile().mkdirs())
                throw new IOException("Unable to create dirs for file");
            if (!f.createNewFile())
                throw new IOException("Unable to create file");
        }/*from   w w  w . j a  v  a  2 s .  co  m*/
        FileWriter w = new FileWriter(f);
        w.write(str);
        w.close();
    }

    public static void close(InputStream is) {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
            }
    }

    public static void close(OutputStream os) {
        if (os != null)
            try {
                os.close();
            } catch (IOException e) {
            }
    }

    public static void close(Reader r) {
        if (r != null)
            try {
                r.close();
            } catch (IOException e) {
            }
    }
}

Related

  1. writeFile(String pContent, String pPath)
  2. writeFile(String ruta, String nombre, byte[] archivo)
  3. WriteFile(String sFileName, String content)
  4. writeFile(String sFileName, String sContent)
  5. writeFile(String sName, String data, String encoding)
  6. writeFile(String str, String filename, boolean append)
  7. writeFile(String string, File file)
  8. writeFile(String string, File location, boolean forceASCII)
  9. writeFile(String tailored, File f)