-
序列化和反序列化
添加时间:2013-5-2 点击量:
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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;
- }
- }