Java File Append appendFile(final String name, final String s)

Here you can find the source of appendFile(final String name, final String s)

Description

Writes to the file with the given name

License

Open Source License

Parameter

Parameter Description
name the file name
s the contents of the file

Exception

Parameter Description
IOException if an I/O problem occurs

Declaration

public final static void appendFile(final String name, final String s) throws IOException 

Method Source Code

//package com.java2s;
/**//from w ww. j  a v  a2 s  .c o  m
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * 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.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import java.io.*;

public class Main {
    private final static boolean DEF_APPEND = false;

    /**
     * Writes to the file with the given name
     * 
     * @param name the file name
     * @param s the contents of the file
     * @throws IOException if an I/O problem occurs
     **/
    public final static void appendFile(final String name, final String s) throws IOException {
        writeFile(name, s, true);
    }

    /**
     * Writes to the file with the given name
     * 
     * @param name the file name
     * @param s the contents of the file
     * @throws IOException if an I/O problem occurs
     **/
    public final static void writeFile(final String name, final String s) throws IOException {
        writeFile(name, s, false);
    }

    /**
     * Writes to the file with the given name
     * 
     * @param name the file name
     * @param s the contents of the file
     * @param append whether data should be appended to the end of the file instead of overwriting
     *            it
     * @throws IOException if an I/O problem occurs
     **/
    public final static void writeFile(final String name, final String s, final boolean append) throws IOException {
        //writer = new BufferedWriter(new FileWriter(name, append));
        final BufferedWriter writer = new BufferedWriter(getFileWriter(name, append));
        writer.write(s);
        writer.close();
    }

    public final static Writer getFileWriter(final String absolutePath) throws IOException {
        return getFileWriter(absolutePath, DEF_APPEND);
    }

    public final static Writer getFileWriter(final String absolutePath, final boolean append) throws IOException {
        return new OutputStreamWriter(getFileOutputStream(absolutePath, append));
    }

    public final static Writer getFileWriter(final File f) throws IOException {
        return getFileWriter(f, DEF_APPEND);
    }

    public final static Writer getFileWriter(final File f, final boolean append) throws IOException {
        return new OutputStreamWriter(getFileOutputStream(f, append));
    }

    /**
     * Retrieves a OutputStream for the given absolute path, creating any necessary directories
     * 
     * @param absolutePath the absolute path
     * @return the OutputStream
     * @throws IOException if an I/O problem occurs
     **/
    public final static OutputStream getFileOutputStream(final String absolutePath) throws IOException {
        return getFileOutputStream(absolutePath, DEF_APPEND);
    }

    /**
     * Retrieves a OutputStream for the given absolute path, creating any necessary directories
     * 
     * @param absolutePath the absolute path
     * @param append whether the file should be opened in append mode
     * @return the OutputStream
     * @throws IOException if an I/O problem occurs
     **/
    public final static OutputStream getFileOutputStream(final String absolutePath, final boolean append)
            throws IOException {
        return getFileOutputStream(new File(absolutePath), append);
    }

    public final static OutputStream getFileOutputStream(final File f) throws IOException {
        return getFileOutputStream(f, DEF_APPEND);
    }

    public final static OutputStream getFileOutputStream(final File f, final boolean append) throws IOException {
        final File dir = f.getParentFile();

        if ((dir != null) && !dir.exists()) {
            dir.mkdirs();
        }

        return new FileOutputStream(f, append);
    }
}

Related

  1. appendFile(File src, File dst)
  2. appendFile(File targetFile, String text)
  3. appendFile(File targetFile, String toWrite)
  4. appendFile(final File srcFile, final File destFile)
  5. appendFile(final String filename, final String content)
  6. appendFile(OutputStream output, File source)
  7. appendFile(String content, File file)
  8. appendFile(String file, byte[]... data)
  9. appendFile(String file, String content)