package serializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectToIO
{
/**
*
* 对象转Byte数组
*
* @param obj
*
* @return
*
* @throws Exception
*
*/
public static byte[] objectToBytes(Object obj) throws Exception
{
// logger.debug("objectToString called ");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream sOut = new ObjectOutputStream(out);
sOut.writeObject(obj);
sOut.flush();
byte[] bytes = out.toByteArray();
// logger.debug(bytes.toString());
return bytes;
}
/**
*
* 字节数组转对象
* @param content
*
* @return
*
* @throws Exception
*
*/
public static Object bytesToObject(byte[] bytes) throws Exception
{
// logger.debug("bytesToObject called ");
// byte转object
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream sIn = new ObjectInputStream(in);
return sIn.readObject();
}
}
轉自:http://wenku.baidu.com/view/1e0a23b465ce050876321373.html
posted on 2011-09-16 10:51
Ke 阅读(5325)
评论(0) 编辑 收藏 所属分类:
java 、
webservice