Java Text File Append add2File(String fileName, String content)

Here you can find the source of add2File(String fileName, String content)

Description

add File

License

Apache License

Declaration

public static int add2File(String fileName, String content) 

Method Source Code


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

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static int add2File(String fileName, String content) {
        File file = new File(fileName);
        try {//from   w ww .  j av a2s  .c  om
            // if the file is not exist, create it!
            if (file.exists() == false) {
                file.createNewFile();

            }
            // the second parameter is 'true' means add contents at the end of
            // the file
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }

    }
}

Related

  1. appendStringToFile(File f, String s)
  2. appendStringToFile(File file, String string)
  3. appendStringToFile(File file, String string)
  4. appendStringToFile(File file, String string)