Android Utililty Methods Object Serialization

List of utility methods to do Object Serialization

Description

The list of methods to do Object Serialization are organized into topic(s).

Method

voidserialize(final File file, final Object o)
serialize
ObjectOutput out = null;
try {
    out = new ObjectOutputStream(new FileOutputStream(file));
    out.writeObject(o);
} finally {
    if (out != null) {
        out.close();
voidsaveObject(Context context, String fileName, Object obj)
save Object
FileOutputStream fos = context.openFileOutput(fileName,
        Context.MODE_PRIVATE);
ObjectOutput out = null;
try {
    out = new ObjectOutputStream(fos);
    out.writeObject(obj);
    out.flush();
} finally {
...
voidwriteObjectToFile(Object obj, String filePath)
write Object To File
writeObjectToFile(obj, filePath, false);
voidwriteObjectToFile(Object obj, String filePath, boolean append)
write Object To File
if (obj == null || filePath == null || filePath.length() == 0)
    return;
try {
    File file = new File(filePath);
    if (!file.exists()) {
        File parent = file.getParentFile();
        if (!parent.exists())
            parent.mkdirs();
...
voidwriteQueueToFile(Queue list, String filePath)
write Queue To File
if (list == null || filePath == null || filePath.length() == 0)
    return;
try {
    File file = new File(filePath);
    if (!file.exists()) {
        file.getParentFile().mkdirs();
        file.createNewFile();
    ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream(file, true));
    Object object = list.poll();
    while (object != null) {
        oos.writeObject(object);
        object = list.poll();
    oos.flush();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
byte[]ObjectToByte(Serializable obj)
Object To Byte
byte[] bytes = null;
try {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(obj);
    bytes = bo.toByteArray();
    bo.close();
    oo.close();
...
byte[]obj2Bytes(Object obj)
obj Bytes
byte[] result = null;
try {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(obj);
    result = bo.toByteArray();
    bo.close();
    oo.close();
...
byte[]obj2Bytes(Object obj)
obj Bytes
byte[] result = null;
try {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(obj);
    result = bo.toByteArray();
    bo.close();
    oo.close();
...
byte[]objectToByte(Object obj)
object To Byte
ObjectOutputStream oos = null;
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    return bos.toByteArray();
} finally {
    if (oos != null)
...
ObjectgetObjectFormFile(String filePath)
get Object Form File
if (filePath == null || filePath.length() == 0)
    return null;
try {
    File file = new File(filePath);
    if (file.exists()) {
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(file));
        Object object = ois.readObject();
...