read Object from byte array - Java java.io

Java examples for java.io:Serializable

Description

read Object from byte array

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.ObjectInputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] result = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(readObject(result));
    }/*from  w  ww.  j a  va 2 s. c  o  m*/

    public static Object readObject(byte[] result) throws IOException,
            ClassNotFoundException {
        ByteArrayInputStream bos = new ByteArrayInputStream(result);
        ObjectInputStream oos = new ObjectInputStream(bos);
        Object obj = oos.readObject();
        bos.close();
        oos.close();
        return obj;
    }
}

Related Tutorials