Java ObjectOutputStream Write saveObject(File file, Serializable obj)

Here you can find the source of saveObject(File file, Serializable obj)

Description

save Object

License

Mozilla Public License

Declaration

public static void saveObject(File file, Serializable obj) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * /*  w  w w . j  a  va2  s. c o  m*/
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: Jsoda
 * The Initial Developer of the Original Code is: William Wong (williamw520@gmail.com)
 * Portions created by William Wong are Copyright (C) 2012 William Wong, All Rights Reserved.
 *
 ******************************************************************************/

import java.io.*;

public class Main {
    public static void saveObject(File file, Serializable obj) throws IOException {
        objToStream(new BufferedOutputStream(new FileOutputStream(file)), obj);
    }

    public static void objToStream(OutputStream os, Serializable obj) throws IOException {
        ObjectOutputStream oo = new ObjectOutputStream(os);
        try {
            oo.writeObject(obj);
        } finally {
            close(oo);
        }
    }

    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(Writer os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(Reader is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignored) {
            }
        }
    }
}

Related

  1. saveFile(Object o, String filename)
  2. saveGZipObject(Object toSave, File file)
  3. saveIndex(String path, Serializable obj)
  4. saveInSerFile(String filename, T object)
  5. saveObject(File file, Object o, boolean overwrite)
  6. saveObject(Object o, File f)
  7. saveObject(Object obj, File file)
  8. saveObject(Object object, File file)
  9. saveObject(Object object, String filename)