Java Text File Write writeStringToFile(String filename, String str)

Here you can find the source of writeStringToFile(String filename, String str)

Description

write String To File

License

Academic Free License

Declaration

public static void writeStringToFile(String filename, String str) 

Method Source Code

//package com.java2s;
//License from project: Academic Free License 

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

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

public class Main {
    public static void writeStringToFile(String filename, String str) {
        writeStringToFile(filename, str, false);
    }//from   www  .j  a v  a 2  s  .  c  o m

    public static void writeStringToFile(String filename, String str, boolean append) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(filename, append);
            fw.write(str);
            fw.flush();
            fw.close();
            fw = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close(fw);
        }
    }

    /**
     * Close the output stream and trap any exceptions.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ex) {
            }
        }
    }

    public static void close(Writer fw) {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception ex) {
            }
            ;
        }
    }

    /**
     * Close the input stream and trap any exceptions.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ex) {
            }
        }
    }

    /**
     * Close the reader.
     * 
     * @param r
     */
    public static void close(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (Exception ex) {

            }
        }
    }
}

Related

  1. writeStringToFile(String file, String data, boolean append)
  2. writeStringToFile(String file, String str, boolean append, boolean newLine)
  3. writeStringToFile(String fileName, String content)
  4. writeStringToFile(String filename, String content)
  5. writeStringToFile(String fileName, String src)
  6. writeStringToFile(String fileName, String str, boolean isAppend)
  7. writeStringToFile(String filename, String string)
  8. writeStringToFile(String filename, String string, boolean overwrite)
  9. writeStringToFile(String filename, String stringToWrite)