Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

In this page you can find the example usage for java.io ObjectOutputStream ObjectOutputStream.

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:Main.java

public static void saveObj(Object obj, String fileName) throws IOException {
    File f = new File(fileName + ".tmp");
    f.getParentFile().mkdirs();//  www .  j av a 2 s.c  o m
    FileOutputStream fos = new FileOutputStream(f);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(obj);
    out.flush();
    out.close();
    fos.flush();
    fos.close();
    File file = new File(fileName);
    file.delete();
    f.renameTo(file);
}

From source file:Main.java

public static boolean saveObject(Context context, Object data, String filename) {
    FileOutputStream out;//  www .ja  v  a 2  s  .c  o  m
    ObjectOutputStream oout;
    try {
        out = context.openFileOutput(filename + ".odb", Context.MODE_PRIVATE);
        oout = new ObjectOutputStream(out);
        oout.writeObject(data);
        oout.flush();
        out.flush();
        oout.close();
        out.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static <T> void writeSerializedObject(Context context, String fileName, T objectToWrite)
        throws IOException {
    FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(objectToWrite);/*from w  ww  . j a v  a 2s.c  om*/
    out.close();
    fileOut.close();
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {/*from   w  ww. j  a  v  a 2  s . c  o m*/
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static void writeObjectToFile(Object object, File file) {
    ObjectOutputStream oos = null;
    try {// ww  w .jav a  2  s.c om
        FileOutputStream fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:KeyTools.java

public static void writeToFile(Key key, File file) throws IOException {
    FileOutputStream fileoutputstream = new FileOutputStream(file);
    ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream);
    objectoutputstream.writeObject(key);
    objectoutputstream.close();/* ww w  .ja v a 2  s .c o  m*/
}

From source file:Main.java

public static List copyBySerialize(List src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);//from  w w w.j av  a 2 s  . c  o  m

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    List dest = (List) in.readObject();
    return dest;
}

From source file:Main.java

public static void saveObj(String fileName, Object obj) {

    try {// ww  w .j av a2  s  .co  m
        FileOutputStream fout = new FileOutputStream(fileName);
        ObjectOutputStream oout = new ObjectOutputStream(fout);
        oout.writeObject(obj);
        fout.close();
        oout.close();
    } catch (Exception e) {
        printE("OpusTool", e);
    }
}

From source file:Main.java

public static byte[] ObjectToByte(Serializable obj) {
    byte[] bytes = null;
    try {// ww w.ja v a 2s .com
        // object to bytearray
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);

        bytes = bo.toByteArray();

        bo.close();
        oo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:Main.java

public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);/* w  ww. j a v a 2 s . c  om*/

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    @SuppressWarnings("unchecked")
    List<T> dest = (List<T>) in.readObject();
    return dest;
}