Java File Append appendFile(String fullName, String text)

Here you can find the source of appendFile(String fullName, String text)

Description

append File

License

Apache License

Declaration

public static boolean appendFile(String fullName, String text) 

Method Source Code


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

import java.io.*;

public class Main {
    public static boolean appendFile(String fullName, String text) {

        try {/*from   w  ww  .  ja  va  2  s .  c  om*/
            newFile(fullName);
            FileWriter out = new FileWriter(fullName, true);
            out.write(text);
            out.close();
        } catch (Exception e) {
            System.out.println("error occur when creating file");
            e.printStackTrace();
            return false;
        }
        return true;

    }

    public static boolean newFile(String fullName) {
        try {
            File file = new File(fullName);
            if (!file.exists()) {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            }
        } catch (Exception e) {
            System.out.println("error occur when creating folder");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean newFile(String fullName, String text) {

        try {
            newFile(fullName);
            FileWriter out = new FileWriter(fullName, false);
            out.write(text);
            out.close();
        } catch (Exception e) {
            System.out.println("error occur when creating file");
            e.printStackTrace();
            return false;
        }
        return true;

    }

    public static void createNewFile(File f) throws IOException {
        if (f.exists()) {
            f.delete();
        }
        f.getParentFile().mkdirs();
        f.createNewFile();
    }
}

Related

  1. appendFile(String fileName, byte[] data)
  2. appendFile(String fileName, String content)
  3. appendFile(String filename, String text)
  4. appendFile(String filename, String text)
  5. appendFile(String fileName, StringBuffer sb)
  6. appendFile(String path, String content)
  7. appendFile(String path, String sb)
  8. appendFile(String source, String filetoappend)
  9. appendFile(String strThrift, String filePath)