Java Write String to File writeFile(File file, String content)

Here you can find the source of writeFile(File file, String content)

Description

write File

License

LGPL

Declaration

public static boolean writeFile(File file, String content) 

Method Source Code

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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static boolean writeFile(File file, String content) {
        if (!file.exists()) {
            try {
                file.createNewFile();//from   w  ww . j ava  2s.co m
            } catch (IOException e) {
                return false;
            }
        }
        FileWriter w = null;
        try {
            w = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(w);
            bw.write(content);
            bw.flush();
            w.flush();
            w.close();
            return true;
        } catch (IOException e) {
            if (w != null) {
                try {
                    w.close();
                } catch (IOException e2) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    }
}

Related

  1. writeFile(File file, String content)
  2. writeFile(File file, String content)
  3. writeFile(File file, String content)
  4. writeFile(File file, String content)
  5. writeFile(File file, String content)
  6. writeFile(File file, String content, String charset)
  7. writeFile(File file, String content, String charsetName)
  8. writeFile(File file, String contents)
  9. writeFile(File file, String contents)