Convert object to byte array - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

Convert object to byte array

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutputStream;

public class Main {

    public static byte[] objectToByte(Object obj) throws Exception {
        ObjectOutputStream oos = null;
        try {//from w  w  w . java2s  .c  om
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            return bos.toByteArray();
        } finally {
            if (oos != null)
                oos.close();
        }
    }
}

Related Tutorials