Java File Append appendTo(String fileName, String str)

Here you can find the source of appendTo(String fileName, String str)

Description

Append a String to a file.

License

Open Source License

Declaration

public static synchronized void appendTo(String fileName, String str) throws NullPointerException, IOException 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * The contents of this file are subject to the Ricoh Source Code Public
 * License Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.risource.org/RPL//  w w  w.  j a v  a2s .  c o m
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * This code was initially developed by Ricoh Innovations, Inc.  Portions
 * created by Ricoh Innovations, Inc. are Copyright (C) 1995-1999.  All
 * Rights Reserved.
 *
 * Contributor(s):
 *
 ***************************************************************************** 
*/

import java.io.*;

public class Main {
    /**
     * Append a String to a file.
     *
     */
    public static synchronized void appendTo(String fileName, String str) throws NullPointerException, IOException {
        RandomAccessFile f = null;

        if (str == null)
            return;

        try {
            f = new RandomAccessFile(fileName, "rw");
            long length = f.length();
            f.seek(length);
            f.write(str.getBytes());
        } catch (NullPointerException e1) {
            // bad file name
            throw e1;
        } catch (IOException e2) {
            throw e2;
        } finally {
            if (f != null)
                try {
                    f.close();
                } catch (IOException e3) {
                    throw e3;
                }
        }
    }
}

Related

  1. appendFileLines(String fileName, String[] data)
  2. appendFilePart(File dstFile, byte[] bytes)
  3. appendHex(A sb, byte[] array, int offset, int len, char sep)
  4. appendHexJavaScriptRepresentation(Appendable out, int codePoint)
  5. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  6. appendTo(StringBuffer sb, CharSequence s)
  7. appendToArray(final File[] original, final File[] toAppend)
  8. appendToCollection(final Vector original, final File[] toAppend)
  9. appendToFileName(File file, String str)