Java Object Save writeObject(File f, Object obj)

Here you can find the source of writeObject(File f, Object obj)

Description

write Object

License

Open Source License

Declaration

public static boolean writeObject(File f, Object obj) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

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

import java.io.ObjectOutputStream;

public class Main {
    public static boolean writeObject(File f, Object obj) {
        boolean retObj = true;

        if (!f.getParentFile().isDirectory()) {
            f.getParentFile().mkdirs();//from   w  ww. j a v  a  2 s .  c o m
        }

        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(f);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(obj);
            objectOutputStream.close();
        } catch (IOException e) {
            retObj = false;
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
            }
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                }
            }
        }

        return retObj;
    }
}

Related

  1. writeObject(File f, Serializable obj)
  2. writeObject(File file, Object obj)
  3. writeObject(File file, Object object)
  4. writeObject(File file, Object object)