Java Text File Append appendStringToFile(String string, String filename)

Here you can find the source of appendStringToFile(String string, String filename)

Description

Writes string at the end of the file represented by filename

License

LGPL

Parameter

Parameter Description
string String to be written
filename File into which the <tt>string</tt> will be wrote

Declaration

public static void appendStringToFile(String string, String filename) 

Method Source Code

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

import java.io.DataOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**//from   w  ww .  j a v a  2 s.  c  o m
     * Writes <tt>string</tt> at the end of the file represented by <tt>filename</tt>
     *
     * @param string String to be written
     * @param filename File into which the <tt>string</tt> will be wrote
     */
    public static void appendStringToFile(String string, String filename) {
        if (string.equals("")) {
            return;
        }
        File destFile = new File(filename);
        try {
            if (!destFile.exists()) {
                destFile.createNewFile();
            }
            FileOutputStream appendedFile = new FileOutputStream(destFile, true);
            DataOutputStream outputStream = new DataOutputStream(appendedFile);
            outputStream.writeBytes("\n");
            outputStream.writeBytes(string);
            appendedFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Given a string representing a file determines whether the file exists.
     * @param filename the string representing the path of the file.
     * @return tru if the file exists, false otherwise.
     */
    public static boolean exists(String filename) {
        File f = new File(filename);
        return f.exists();
    }
}

Related

  1. appendStringToFile(File file, String string)
  2. appendStringToFile(final String path, final String output)
  3. appendStringToFile(String file, String contents)
  4. appendStringToFile(String fileName, String data)
  5. appendStringToFile(String str, String oFileName)
  6. FileAppend(String fileNameWithPath, String content)
  7. fileAppend(String target, String source)
  8. fileAppendString(String target, String source)