serialize object to byte array - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

serialize object to byte array

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutputStream;

import android.util.Log;

public class Main {
    public static byte[] serialize(Object obj) {
        try {/*w w w.  j  a v a 2 s  .c o m*/
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(out);

            os.writeObject(obj);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials