Java Object Serialize serializeObject(Object obj, String dir)

Here you can find the source of serializeObject(Object obj, String dir)

Description

serialize Object

License

Open Source License

Declaration

public static void serializeObject(Object obj, String dir) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w w  w .j a v a 2 s  . co  m*/
 * Musite
 * Copyright (C) 2010 Digital Biology Laboratory, University Of Missouri
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.zip.GZIPOutputStream;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void serializeObject(Object obj, String dir) throws IOException {
        if (obj == null || dir == null) {
            throw new NullPointerException();
        }

        FileOutputStream fos = new FileOutputStream(dir);
        ObjectOutputStream out;
        if (dir.endsWith(".gz")) {
            GZIPOutputStream gzos = new GZIPOutputStream(fos);
            out = new ObjectOutputStream(gzos);
        } else if (dir.endsWith(".zip")) {
            ZipOutputStream zos = new ZipOutputStream(fos);
            out = new ObjectOutputStream(zos);
        } else {
            out = new ObjectOutputStream(fos);
        }

        out.writeObject(obj);
        out.close();
    }

    public static void serializeObject(Object obj, String dir, String format) throws IOException {
        if (obj == null || dir == null) {
            throw new NullPointerException();
        }

        FileOutputStream fos = new FileOutputStream(dir);
        ObjectOutputStream out;
        if (format.equalsIgnoreCase("gz")) {
            GZIPOutputStream gzos = new GZIPOutputStream(fos);
            out = new ObjectOutputStream(gzos);
        } else if (format.equalsIgnoreCase("zip")) {
            ZipOutputStream zos = new ZipOutputStream(fos);
            out = new ObjectOutputStream(zos);
        } else {
            out = new ObjectOutputStream(fos);
        }

        out.writeObject(obj);
        out.close();
    }
}

Related

  1. serializeObject(Object data)
  2. serializeObject(Object myObject)
  3. serializeObject(Object o)
  4. serializeObject(Object obj)
  5. serializeObject(Object obj, String destPath)
  6. serializeObject(Object object)
  7. serializeObject(Object object)
  8. serializeObject(Object object, File f)
  9. serializeObject(Object serializedObject)