Java File Append appendFile(String file, String text)

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

Description

append File

License

Open Source License

Declaration

public static int appendFile(String file, String text) 

Method Source Code


//package com.java2s;
import java.io.BufferedWriter;

import java.io.FileWriter;

public class Main {

    public static int appendFile(String file, String text) {
        return appendFile(file, text, null);
    }/* ww w. ja  v a  2s  .c o m*/

    public static int appendFile(String file, String text, String encoding) {
        int rtn = 0;
        try {
            FileWriter fw = new FileWriter(file, true);
            if (fw != null) {
                BufferedWriter bw = new BufferedWriter(fw);
                if (encoding == null) {
                    bw.append(text);
                } else {
                    bw.append(new String(text.getBytes(), encoding));
                }
                bw.flush();
                bw.close();
                fw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            rtn = 1;
        }
        return rtn;
    }
}

Related

  1. appendFile(final String name, final String s)
  2. appendFile(OutputStream output, File source)
  3. appendFile(String content, File file)
  4. appendFile(String file, byte[]... data)
  5. appendFile(String file, String content)
  6. appendFile(String fileContents, File toFile)
  7. appendFile(String fileName, byte[] data)
  8. appendFile(String fileName, String content)
  9. appendFile(String filename, String text)