基本概念:

序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。

这两个过程结合起来,能够轻松地存储和数据传输。

特别在网络传输中,它的作用显得尤为重要。我们能够把一个类实现序列化,然后在还有一端通过反序列化能够得到该对象





比如:我们能够序列化一个对象。只是这个对象要实现序列化方法,并生成序列化号。

这是对一个对象进行序列化和反序列化的过程:

public static byte[] serializeObj(Object object){
if (object == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(object);
os.writeObject(null);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
if (bos != null) {
bos.flush();
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return rv;
} @SuppressWarnings("unchecked")
public static <T>T deserializeObj(byte[] in,Class<T> clazz) {
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
while (true) {
T o = (T) is.readObject();
if (o == null) {
break;
} else {
return o;
}
} }
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

以下是一个扩展(对集合的序列化和反序列化):

public static <T>byte[] serializeList(List<T> value ,Class<T> clazz) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
for (T t : value) {
os.writeObject(t);
}
os.writeObject(null);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
if (bos != null) {
bos.flush();
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return rv;
} @SuppressWarnings("unchecked")
public static <T> List<T> deserializeList(byte[] in,Class<T> clazz) {
List<T> list = new ArrayList<T>();
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
while (true) {
T o = (T) is.readObject();
if (o == null) {
break;
} else {
list.add(o);
}
} }
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

最新文章

  1. Android文件下载之进度检测
  2. Sass-也许你想和CSS玩耍起来(上篇)
  3. python 自学 1 day
  4. 在VS中添加lib的简单方法
  5. Protocol Buffers动态消息解析
  6. PHP中用下划线开头的变量含义
  7. PLSQL 的简单命令之五
  8. Oracle 中union的用法
  9. pythonchallenge关卡破解
  10. IoC~高效的Autofac
  11. ios开发之NavBar和TarBar使用技巧
  12. 51单片机模拟I2C总线的C语言实现
  13. mysql命令行里的加载更多显示
  14. NDK配置debug环境时:Error:FAILURE: Build failed with an exception
  15. 扒一扒.NET Core的环境配置提供程序
  16. 防止用户重发发生ajax请求
  17. 第44节:Java当中的JVM
  18. Mysql:性能优化
  19. Shell data、timedatectl
  20. Codeforces gym 102062 简要题解

热门文章

  1. ubuntu-虚拟机分辨率设定
  2. Codefroces 760 B. Frodo and pillows
  3. web存储方法,现成代码
  4. numpy_basic3
  5. C++ 与C# 对应的变量互转
  6. 洛谷 P3669 [USACO17OPEN]Paired Up 牛牛配对
  7. 让人难过的 openssl_pkcs7_encrypt
  8. ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第三篇:ASP.NET MVC全局观
  9. 【Codeforces Round #450 (Div. 2) C】Remove Extra One
  10. 【例题 7-2 UVA - 11059】Maximum Product