Java Text File Write writeStringToFile(File f, String content, boolean append)

Here you can find the source of writeStringToFile(File f, String content, boolean append)

Description

write String To File

License

Open Source License

Declaration

public static boolean writeStringToFile(File f, String content, boolean append) 

Method Source Code


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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

public class Main {
    static final Logger log = Logger.getLogger("Utils");

    public static boolean writeStringToFile(File f, String content, boolean append) {
        PrintWriter fout = null;//from   w  ww . j  a  v  a2s  . c  o  m
        try {
            fout = new PrintWriter(new BufferedWriter(new FileWriter(f, append)));
            fout.print(content);
            fout.flush();
            fout.close();
            return true;
        } catch (FileNotFoundException e) {
            warn("File " + f + " not found: " + e.getMessage());
        } catch (IOException e) {
            warn("IO Exception: " + e.getMessage());
        } finally {
            if (fout != null) {
                fout.flush();
                fout.close();
            }
        }
        return false;
    }

    private static void warn(String msg) {
        log.warning(msg);
    }
}

Related

  1. writeStringListToFile(File file, List lines)
  2. writeStringTo(File outFile, String data)
  3. writeStringTo(String _value, File _target)
  4. writeStringToFile(@Nonnull String string, @Nonnull final File file)
  5. writeStringToFile(CharSequence contents, String filename)
  6. writeStringToFile(File f, String s)
  7. writeStringToFile(File f, String s)
  8. writeStringToFile(File f, String s)
  9. writeStringToFile(File file, String content)