序列化和反序列化
添加时间:2013-5-2 点击量:
- /
- 对象序列化
- @param obj
- @return
- /
- public byte[] ObjectToByte(Object obj) {
- byte[] bytes = null;
- try {
- ByteArrayOutputStream bo = new ByteArrayOutputStream();
- ObjectOutputStream oo = new ObjectOutputStream(bo);
- oo.writeObject(obj);
- bytes = bo.toByteArray();
- bo.close();
- oo.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- return bytes;
- }
-
- /
- 反序列化
- @param bytes
- @return
- /
- public Object ByteToObject(byte[] bytes) {
- Object object = null;
- try {
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = new ObjectInputStream(bais);
- object = ois.readObject();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return object;
- }
- }
- /
- 对象序列化
- @param obj
- @return
- /
- public byte[] ObjectToByte(Object obj) {
- byte[] bytes = null;
- try {
- ByteArrayOutputStream bo = new ByteArrayOutputStream();
- ObjectOutputStream oo = new ObjectOutputStream(bo);
- oo.writeObject(obj);
- bytes = bo.toByteArray();
- bo.close();
- oo.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- return bytes;
- }
- /
- 反序列化
- @param bytes
- @return
- /
- public Object ByteToObject(byte[] bytes) {
- Object object = null;
- try {
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = new ObjectInputStream(bais);
- object = ois.readObject();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return object;
- }
- }