Java Text File Write writeStringToFile(String filename, String string, boolean overwrite)

Here you can find the source of writeStringToFile(String filename, String string, boolean overwrite)

Description

write String To File

License

LGPL

Parameter

Parameter Description
filename a parameter
strings a parameter
overwrite a parameter

Declaration

public static void writeStringToFile(String filename, String string, boolean overwrite) 

Method Source Code


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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Main {
    /**/*from   ww  w  . ja  va 2s.co m*/
     * @param filename
     * @param strings
     * @param overwrite
     */
    public static void writeStringToFile(String filename, String string, boolean overwrite) {
        File destFile = new File(filename);
        try {
            if (destFile.exists()) {
                if (overwrite)
                    destFile.delete();
                else
                    return;
            }
            destFile.createNewFile();
            Writer outputStream = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"));

            outputStream.write(string);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeStringToFile(String filename, String content)
  2. writeStringToFile(String fileName, String src)
  3. writeStringToFile(String filename, String str)
  4. writeStringToFile(String fileName, String str, boolean isAppend)
  5. writeStringToFile(String filename, String string)
  6. writeStringToFile(String filename, String stringToWrite)
  7. writeStringToFile(String filename, String text)
  8. WriteStringToFile(String fn, String dest)
  9. writeStringToFile(String fullPath, byte[] fileBytes)