Here you can find the source of save(Object o)
Parameter | Description |
---|---|
o | the object to be stored in the file |
public static byte[] save(Object o)
//package com.java2s; /*/* w w w. j av a2 s . c om*/ * Copyright 2009 Keith Stevens * * This file is part of the S-Space package and is covered under the terms and * conditions therein. * * The S-Space package is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as published * by the Free Software Foundation and distributed hereunder to you. * * THIS SOFTWARE IS PROVIDED "AS IS" AND NO REPRESENTATIONS OR WARRANTIES, * EXPRESS OR IMPLIED ARE MADE. BY WAY OF EXAMPLE, BUT NOT LIMITATION, WE MAKE * NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION * WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER * RIGHTS. * * 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.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOError; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; public class Main { /** * Serializes the object to the provided file. * * @param o the object to be stored in the file * @param file the file in which the object should be stored */ public static void save(Object o, String file) { save(o, new File(file)); } /** * Serializes the object to the provided file. * * @param o the object to be stored in the file * @param file the file in which the object should be stored */ public static void save(Object o, File file) { try { FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream outStream = new ObjectOutputStream(new BufferedOutputStream(fos)); outStream.writeObject(o); outStream.close(); } catch (IOException ioe) { throw new IOError(ioe); } } /** * Serializes the object to the provided stream. This method does not close * the stream after writing. * * @param o the object to be stored in the file * @param stream the stream in which the object should be stored */ public static void save(Object o, OutputStream stream) { try { ObjectOutputStream outStream = (stream instanceof ObjectOutputStream) ? (ObjectOutputStream) stream : new ObjectOutputStream(stream); outStream.writeObject(o); } catch (IOException ioe) { throw new IOError(ioe); } } /** * Serializes the object to a {@code byte} array. * * @param o the object to be stored in the file */ public static byte[] save(Object o) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); save(o, baos); return baos.toByteArray(); } }