Java File Append appendFile(String fileContents, File toFile)

Here you can find the source of appendFile(String fileContents, File toFile)

Description

This method is to append the given contents into a file.

License

Open Source License

Parameter

Parameter Description
fileContents contents which are appended to the file.
toFile a file to append contents.

Exception

Parameter Description
IOException ,exception while writing contents into a file

Declaration

public static void appendFile(String fileContents, File toFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  ww  . j  ava  2  s  . c  o m*/
 * Bala Torati (Symbian) - Initial API and implementation
 * Mark Espiritu (VastSystems) - bug 215283
 *******************************************************************************/

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {
    /**
     * This method is to append the given contents into a file.
     * 
     * @param fileContents contents which are appended to the file.
     * @param toFile a file to append contents.
     * @throws IOException,
     *             exception while writing contents into a file
      * 
      * @since 4.0
     */
    public static void appendFile(String fileContents, File toFile) throws IOException {
        RandomAccessFile raf = null;
        if (!toFile.exists()) {
            throw new FileNotFoundException(" The specified destination file does not exists "); //$NON-NLS-1$
        } else {
            try {
                raf = new RandomAccessFile(toFile, "rw"); //$NON-NLS-1$
                raf.skipBytes((int) raf.length());
                raf.writeBytes(fileContents);
            } finally {
                if (raf != null) {
                    raf.close();
                }
            }
        }
    }
}

Related

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