Java Object Save writeObjectToFile(T object, File fileHandle)

Here you can find the source of writeObjectToFile(T object, File fileHandle)

Description

Write a serializable object to a file

License

Apache License

Parameter

Parameter Description
T a parameter
object a parameter
fileHandle a parameter

Return

true if the file is written successfully

Declaration

public static <T extends Object> boolean writeObjectToFile(T object, File fileHandle) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

public class Main {
    /**//  ww w . j  a v  a 2s . co  m
     * Write a serializable object to a file
     * @param <T>
     * @param object
     * @param fileHandle
     * @return   true if the file is written successfully
     */
    public static <T extends Object> boolean writeObjectToFile(T object, File fileHandle) {
        try {
            FileOutputStream fos = new FileOutputStream(fileHandle);
            ObjectOutputStream output = new ObjectOutputStream(fos);
            output.writeObject(object);
            output.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. writeObjectToFile(String dirname, String filename, Object obj)
  2. writeObjectToFile(String fileName, Object obj)
  3. writeObjectToFile(String filename, Serializable s)
  4. writeObjectToFile(String filename, T object)
  5. writeObjectToFile(String outputFileName, Object toWrite, boolean append)
  6. writeObjectToFileNoExceptions(Object o, String filename)