Java Write String to File writeFile(String filePath, String fileData, boolean isAppend)

Here you can find the source of writeFile(String filePath, String fileData, boolean isAppend)

Description

writes file data

License

Apache License

Parameter

Parameter Description
filePath a parameter
fileData a parameter
isAppend a parameter

Declaration

public static int writeFile(String filePath, String fileData, boolean isAppend) 

Method Source Code

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

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

public class Main {
    public static String LAST_ERROR = "";

    /**/* w ww.j a v  a 2 s .  c  o m*/
     * writes file data
     * 
     * @param filePath
     * @param fileData
     * @param isAppend
     * @return
     */
    public static int writeFile(String filePath, String fileData, boolean isAppend) {
        try {
            // create file 
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
                file.createNewFile();
            }
            // write to file 
            BufferedWriter out = new BufferedWriter(new FileWriter(filePath, isAppend));
            out.write(fileData);
            out.close();
            // done 
            return 1;
        } catch (Exception e) {
            LAST_ERROR = e.getMessage();
            e.printStackTrace();
            return -1;
        }

    }
}

Related

  1. writeFile(String filePath, String content)
  2. WriteFile(String filepath, String content)
  3. writeFile(String filePath, String content, boolean append)
  4. writeFile(String filePath, String contents)
  5. writeFile(String filePath, String fileContent)
  6. writeFile(String filePath, String fileName, String[] args)
  7. writeFile(String filePath, String templetContent, String charsetName)
  8. writeFile(String filePath, String text)