Java Serializable Serialize serializeObject(File inFile, Serializable inObject)

Here you can find the source of serializeObject(File inFile, Serializable inObject)

Description

serialize Object

License

Open Source License

Declaration

public static void serializeObject(File inFile, Serializable inObject) throws IOException 

Method Source Code

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

import java.io.Closeable;
import java.io.File;

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

import java.io.ObjectOutputStream;
import java.io.OutputStream;

import java.io.Serializable;

import java.io.Writer;

import java.util.Collection;

public class Main {
    public static void serializeObject(File inFile, Serializable inObject) throws IOException {
        serializeObject(new FileOutputStream(inFile), inObject, true);
    }/*from w  w w .jav a 2 s .c o m*/

    public static void serializeObject(OutputStream inOutputStream, Serializable inObject, boolean inCloseStream)
            throws IOException {

        if (inObject == null) {
            return;
        }

        ObjectOutputStream theOs = null;

        try {

            theOs = new ObjectOutputStream(inOutputStream);
            theOs.writeObject(inObject);
            theOs.flush();

        } finally {

            if (inCloseStream) {

                close(theOs);
            }
        }

    }

    public static void flush(OutputStream inOutputStream) {

        try {

            inOutputStream.flush();

        } catch (final IOException e) {
            // swallow
        }
    }

    public static void flush(Writer inWriter) {

        try {

            inWriter.flush();

        } catch (final IOException e) {
            // swallow
        }
    }

    public static void flush(Object inOut) {

        if (inOut instanceof OutputStream) {

            flush((OutputStream) inOut);
        } else if (inOut instanceof Writer) {

            flush((Writer) inOut);
        }
    }

    public static void close(Closeable inCloseable) {
        close(inCloseable, false);
    }

    public static void close(Closeable inCloseable, boolean inFlush) {

        if (inFlush) {
            flush(inCloseable);
        }

        try {

            inCloseable.close();

        } catch (final Exception e) {
        }

    }

    public static void close(Closeable... inCloseable) {

        if (inCloseable != null) {

            for (final Closeable closeable : inCloseable) {

                close(closeable);
            }
        }
    }

    public static void close(Collection<Closeable> inCloseables) {

        if (inCloseables != null) {

            for (final Closeable closeable : inCloseables) {

                close(closeable);
            }
        }
    }
}

Related

  1. serializeObject(Serializable cds)
  2. serializeObject(Serializable o)
  3. serializeObject(Serializable obj, String filename)
  4. serializeObject(Serializable ser)