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 writeDataFile(String filename, Context context, Object object) {
    File file = new File(context.getDir("data", 0), filename);
    try {/*  ww  w . j a va 2 s.c o  m*/
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false));
        outputStream.writeObject(object);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] serialize(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;//from w w w  .  j a  v  a 2 s  . c o m
    byte[] bytes = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        bytes = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return bytes;
}

From source file:Main.java

public static boolean saveObject(Context cxt, Serializable obj, String file) {
    FileOutputStream fos = null;/*from  w ww. j  a  v a  2  s  .c o m*/
    ObjectOutputStream oos = null;

    try {
        fos = cxt.openFileOutput(file, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            oos.close();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void writeObject(String root, String filename, Object object) {
    File file = new File(root, filename);
    try {/* w  w w  .j a va2 s . co  m*/
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.flush();
        oos.close();
    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to write [%s]", file), e);
    }
}

From source file:Main.java

public static void saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;// w w w.  j  ava2  s. co  m
    try {
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

From source file:Main.java

public static String objectToString(Serializable object) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/* w w w.  j a v a 2 s .co  m*/
        new ObjectOutputStream(out).writeObject(object);
        byte[] data = out.toByteArray();
        out.close();

        out = new ByteArrayOutputStream();
        Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
        b64.write(data);
        b64.close();
        out.close();

        return new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * serialize to file//from   w w  w  .j a va 2s. c  o m
 * 
 * @param filePath
 * @param obj
 * @return
 * @throws RuntimeException if an error occurs
 */
public static void serialization(String filePath, Object obj) {
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new FileOutputStream(filePath));
        out.writeObject(obj);
        out.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("FileNotFoundException occurred. ", e);
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:Main.java

public static String objectSerializableToString(Serializable object) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream os = null;
    try {/*from   w ww . ja  va 2 s . c o m*/
        if (object != null) {
            baos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(baos);
            os.writeObject(object);
            os.flush();
            baos.flush();
            return baos.toString("UTF-8");
        }
    } catch (Throwable e) {
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (Throwable e2) {
        }

        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Throwable e2) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * Convert Object to Byte Array/*from   w  w  w.  java 2  s .  c o  m*/
 * 
 * @param obj
 * @return
 * @throws IOException
 */
public static byte[] convertObjectToByteArray(Object obj) throws IOException {
    ObjectOutputStream os = null;
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
    os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
    os.flush();
    os.writeObject(obj);
    os.flush();
    byte[] sendBuf = byteStream.toByteArray();
    os.close();
    return sendBuf;

}

From source file:Main.java

public static synchronized void writeArrayListToFile(ArrayList<double[]> x, String filename) {
    //        double  []x  = {1,2,4};
    //        String filename = "data.dat";
    ObjectOutput out = null;/*from   ww  w . ja va2 s.  c o  m*/

    try {
        out = new ObjectOutputStream(new FileOutputStream(new File(filename)));
        out.writeObject(x);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}