Java Utililty Methods Write String to File

List of utility methods to do Write String to File

Description

The list of methods to do Write String to File are organized into topic(s).

Method

voidwriteFile(String filePath, String content, boolean append)
write File
File file = new File(filePath);
if (createFile(filePath)) {
    BufferedWriter bufferedWriter = null;
    try {
        FileWriter fileWriter = new FileWriter(file, append);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(content);
        bufferedWriter.flush();
...
voidwriteFile(String filePath, String contents)
write File
writeFile(filePath, contents, true);
booleanwriteFile(String filePath, String fileContent)
write File
FileWriter fw = null;
try {
    fw = new FileWriter(filePath, true);
    PrintWriter pw = new PrintWriter(fw);
    pw.print(fileContent);
    pw.close();
    fw.close();
    return true;
...
intwriteFile(String filePath, String fileData, boolean isAppend)
writes file data
try {
    File file = new File(filePath);
    if (!file.exists()) {
        file.mkdirs();
        file.createNewFile();
    BufferedWriter out = new BufferedWriter(new FileWriter(filePath, isAppend));
    out.write(fileData);
...
voidwriteFile(String filePath, String fileName, String[] args)
write File
File f = new File(filePath);
if (!f.exists()) {
    f.mkdirs();
FileWriter fw = new FileWriter(filePath + fileName);
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i < args.length; i++) {
    out.write(args[i]);
...
booleanwriteFile(String filePath, String templetContent, String charsetName)
write File
boolean isSucc = false;
try {
    if (filePath != null && !filePath.equals("")) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (file.exists() == false) {
            file.mkdirs();
    FileOutputStream fout = new FileOutputStream(filePath);
    OutputStreamWriter out = new OutputStreamWriter(new BufferedOutputStream(fout), charsetName);
    out.write(templetContent);
    out.close();
    fout.close();
    isSucc = true;
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
return isSucc;
voidwriteFile(String filePath, String text)
write File
BufferedWriter bw = null;
try {
    bw = new BufferedWriter(new FileWriter(filePath, true));
    bw.write(text);
    bw.flush();
} catch (IOException e) {
    e.printStackTrace();
} finally {
...