Java Object Serialize serialize(Object object, String fileName)

Here you can find the source of serialize(Object object, String fileName)

Description

Serialize an object into a file if the file does not exist yet

License

Open Source License

Declaration

public static boolean serialize(Object object, String fileName) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

public class Main {
    /**//  w  ww.jav  a 2s .  c  om
     * Serialize an object into a file if the file does not exist yet
     */
    public static boolean serialize(Object object, String fileName) throws IOException {

        File file = new File(fileName);
        if (file.exists())
            return false;
        else {
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            ObjectOutputStream oos = null;
            try {
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                oos = new ObjectOutputStream(bos);
                oos.writeObject(object);
                oos.flush();
                return true;
            } catch (IOException e) {
                file.delete();
                throw e;
            } finally {
                if (oos != null)
                    oos.close();
                if (bos != null)
                    bos.close();
                if (fos != null)
                    fos.close();
            }
        }
    }
}

Related

  1. serialize(Object object)
  2. serialize(Object object)
  3. serialize(Object object, boolean zipped)
  4. serialize(Object object, File file)
  5. serialize(Object object, File file)
  6. serialize(Object object, String path)
  7. Serialize(Object serializableObject, String filePath)
  8. serialize(Object state)
  9. serialize(Object value)