ITeye博客

对象与字节数组之间的转换

chengzhi_hong · 2011-07-05 15:15 · J2SE · 编程语言 · Markdown

将对象转换成字节数组

public byte[] object2Byte(Object obj)throws IOException{
            ByteOutputStream bos = new ByteOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            return bos.toByteArray();           
    }
将字节数组转换成对象  
    public Object byte2Object(byte[] bytes) throws IOException, ClassNotFoundException{
        ByteInputStream bis = new ByteInputStream();
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Object)ois.readObject();
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。