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

private static void writeToFile(String filename, Object object) throws Exception {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(filename)));
    oos.writeObject(object);//  w  ww  .ja v a 2  s .  c o m
    oos.flush();
    oos.close();
}

From source file:Main.java

public static byte[] getBytes(Object obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);//from  ww w.j a  va  2 s. c o  m
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:Main.java

public static final byte[] serialize(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);//from   w  ww. ja va  2s. c  o  m
    oos.flush();
    oos.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] toByteArray(Object obj) {
    byte[] returnBytes = null;
    ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
    ObjectOutput objectOut = null;
    try {/*from   w w w. ja  va 2 s.  c  om*/
        objectOut = new ObjectOutputStream(byteArrayOutStream);
        objectOut.writeObject(obj);
        returnBytes = byteArrayOutStream.toByteArray();
    } catch (java.io.IOException ioe) {
        return null;
    } finally {
        try {
            objectOut.close();
            byteArrayOutStream.close();
        } catch (java.io.IOException ioe) {

        }
    }
    return returnBytes;
}

From source file:Main.java

public static byte[] objectToStream(Object obj) {
    try {/*  ww  w.  jav a2  s. c o  m*/
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(buff);
        stream.writeObject(obj);
        stream.close();
        return buff.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

static byte[] serialize(Serializable obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);//ww  w  . ja v a2  s .  co  m
    return b.toByteArray();
}

From source file:Main.java

public static byte[] serialize(Object obj) throws Exception {
    //System.err.println(" ser ##"+obj);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(obj);//  w  ww . j  av a  2s. c  om
    oout.flush();
    byte array[] = bout.toByteArray();
    oout.close();
    bout.close();
    //System.err.println(" ser #"+new String(array));
    return array;
}

From source file:Main.java

public static byte[] objectToByte(Object obj) throws Exception {
    ObjectOutputStream oos = null;
    try {//w w w.j  a  va 2 s.co  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        return bos.toByteArray();
    } finally {
        if (oos != null)
            oos.close();
    }
}

From source file:Main.java

public static String objectToString(Object obj) {
    try {/*from  w w  w.ja  v a2s .  c  o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(obj);
        String bytesToHexString = bytesToHexString(bos.toByteArray());
        return bytesToHexString;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static boolean writeFileByObject(String fileName, Object object) {
    try {//from www. ja va2s  . c  o  m
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.flush();
        oos.close();
        fos.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}