Java Object Save writeObjectToFile(String outputFileName, Object toWrite, boolean append)

Here you can find the source of writeObjectToFile(String outputFileName, Object toWrite, boolean append)

Description

Writes an object, using its 'toString' method, to the named file.

License

Apache License

Parameter

Parameter Description
outputFileName The name of the file to write the output to.
toWrite The object to write to the file.
append <tt>true</tt> to append to the file, <tt>false</tt> to overwrite.

Declaration

public static void writeObjectToFile(String outputFileName, Object toWrite, boolean append) 

Method Source Code

//package com.java2s;
/*/*  ww w.j av a2s .  c om*/
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Main {
    /**
     * Writes an object, using its 'toString' method, to the named file. The object may optionally be appended to the
     * file, or may overwrite it.
     *
     * @param outputFileName The name of the file to write the output to.
     * @param toWrite        The object to write to the file.
     * @param append         <tt>true</tt> to append to the file, <tt>false</tt> to overwrite.
     */
    public static void writeObjectToFile(String outputFileName, Object toWrite, boolean append) {
        // Open the output file.
        Writer resultWriter;

        try {
            resultWriter = new FileWriter(outputFileName, append);
        } catch (IOException e) {
            throw new IllegalStateException("Unable to open the output file '" + outputFileName + "' for writing.",
                    e);
        }

        // Write the object into the output file.
        try {
            resultWriter.write(toWrite.toString());
            resultWriter.flush();
            resultWriter.close();
        } catch (IOException e) {
            throw new IllegalStateException(
                    "There was an error whilst writing to the output file '" + outputFileName + "'.", e);
        }
    }
}

Related

  1. writeObjectToFile(Object oObject, File destDir, String filename)
  2. writeObjectToFile(String dirname, String filename, Object obj)
  3. writeObjectToFile(String fileName, Object obj)
  4. writeObjectToFile(String filename, Serializable s)
  5. writeObjectToFile(String filename, T object)
  6. writeObjectToFile(T object, File fileHandle)
  7. writeObjectToFileNoExceptions(Object o, String filename)