---
title: "对象与字节数组之间的转换"
author: "chengzhi_hong"
date: "2011-07-05T15:15:01Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-07-08T11:29:02Z"
id: 1114949
url: "https://wamp.iteye.com/blog/1114949"
---

将对象转换成字节数组

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();  
    }