Java Text File Append appendStringToFile(String str, String oFileName)

Here you can find the source of appendStringToFile(String str, String oFileName)

Description

Method to append a string (str) to the end of a file (oFileName).

License

Open Source License

Parameter

Parameter Description
str a string to write to the output file
oFileName the output filename

Exception

Parameter Description
FileNotFoundException if the file exists but is a directory rather than a regularfile, does not exist but cannot be created, or cannot beopened for any other reason
IOException if an I/O error occurs

Declaration

public static void appendStringToFile(String str, String oFileName) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*/*  w  w  w.  ja  v a2s .  co m*/
 * DAITSS Copyright (C) 2007 University of Florida
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.IOException;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class Main {
    /**
     * Method to append a string (str) to the end of a file (oFileName). If the
     * output file does not exist, an attempt will be made to create it.
     * 
     * @param str
     *            a string to write to the output file
     * @param oFileName
     *            the output filename
     * @throws FileNotFoundException
     *             if the file exists but is a directory rather than a regular
     *             file, does not exist but cannot be created, or cannot be
     *             opened for any other reason
     * @throws IOException
     *             if an I/O error occurs
     */
    public static void appendStringToFile(String str, String oFileName) throws FileNotFoundException, IOException {
        FileOutputStream ostream;
        PrintWriter pw;

        ostream = new FileOutputStream(oFileName, true);

        pw = new PrintWriter(ostream);
        pw.println(str);
        pw.close();
        ostream.close();
    }
}

Related

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