1、序列化:序列化后保存在一个字符串变量中
package com.lxh.ser.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class SerializeTest {
private static final String TEMP_ENCODING = "ISO-8859-1";
private static final String DEFAULT_ENCODING = "UTF-8";
public static void main(String[] args) throws IOException {
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("ABC");
System.out.println(writeToStr(list));
}
<span style="white-space:pre"> /**
<span style="white-space:pre"> </span> * 将序列化的字符串反序列化成对象
<span style="white-space:pre"> </span> * @param serStr 系列化的字符串
<span style="white-space:pre"> </span> * @return Object 反序列化后得到原始的对象
<span style="white-space:pre"> </span> * @throws IOException
<span style="white-space:pre"> </span> */</span>
public static String writeToStr(Object obj) throws IOException {
// 此类实现了一个输出流,其中的数据被写入一个 byte 数组。
// 缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 专用于java对象序列化,将对象进行序列化
ObjectOutputStream objectOutputStream = null;
String serStr = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
serStr = byteArrayOutputStream.toString(TEMP_ENCODING);
serStr = java.net.URLEncoder.encode(serStr, DEFAULT_ENCODING);
} catch (IOException e) {
e.printStackTrace();
} finally {
objectOutputStream.close();
}
return serStr;
}
}
2、反序列化:将一个序列化后的字符串反序列化为原有对象
package com.lxh.ser.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class SerializeTest {
<span style="white-space:pre"> </span>private static final String TEMP_ENCODING = "ISO-8859-1";
<span style="white-space:pre"> </span>private static final String DEFAULT_ENCODING = "UTF-8";
<span style="white-space:pre"> </span>public static void main(String[] args) throws IOException {
<span style="white-space:pre"> </span>List<String> list = new ArrayList<String>();
<span style="white-space:pre"> </span>list.add("abc");
<span style="white-space:pre"> </span>list.add("ABC");
<span style="white-space:pre"> </span>String serStr = writeToStr(list);
<span style="white-space:pre"> </span>System.out.println(serStr);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>List<String> deList = (List<String>) deserializeFromStr(serStr);
<span style="white-space:pre"> </span>System.out.println(deList.get(0) + " " + deList.get(1));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 将序列化的字符串反序列化成对象
<span style="white-space:pre"> </span> * @param serStr 系列化的字符串
<span style="white-space:pre"> </span> * @return Object 反序列化后得到原始的对象
<span style="white-space:pre"> </span> * @throws IOException
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public static Object deserializeFromStr(String serStr) throws IOException {
<span style="white-space:pre"> </span>ByteArrayInputStream byteArrayInputStream = null;
<span style="white-space:pre"> </span>ObjectInputStream objectInputStream = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>String deserStr = java.net.URLDecoder.decode(serStr, DEFAULT_ENCODING);
<span style="white-space:pre"> </span>byteArrayInputStream = new ByteArrayInputStream(deserStr.getBytes(TEMP_ENCODING));
<span style="white-space:pre"> </span>objectInputStream = new ObjectInputStream(byteArrayInputStream);
<span style="white-space:pre"> </span>return objectInputStream.readObject();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (ClassNotFoundException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} finally {
<span style="white-space:pre"> </span>objectInputStream.close();
<span style="white-space:pre"> </span>byteArrayInputStream.close();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return null;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 将对象序列化成字符串
<span style="white-space:pre"> </span> * @param obj 需要进行序列化的对象
<span style="white-space:pre"> </span> * @return String 把对象序列化成字符串
<span style="white-space:pre"> </span> * @throws IOException
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public static String writeToStr(Object obj) throws IOException {
<span style="white-space:pre"> </span>// 此类实现了一个输出流,其中的数据被写入一个 byte 数组。
<span style="white-space:pre"> </span>// 缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
<span style="white-space:pre"> </span>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
<span style="white-space:pre"> </span>// 专用于java对象序列化,将对象进行序列化
<span style="white-space:pre"> </span>ObjectOutputStream objectOutputStream = null;
<span style="white-space:pre"> </span>String serStr = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
<span style="white-space:pre"> </span>objectOutputStream.writeObject(obj);
<span style="white-space:pre"> </span>serStr = byteArrayOutputStream.toString(TEMP_ENCODING);
<span style="white-space:pre"> </span>serStr = java.net.URLEncoder.encode(serStr, DEFAULT_ENCODING);
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} finally {
<span style="white-space:pre"> </span>objectOutputStream.close();
<span style="white-space:pre"> </span>byteArrayOutputStream.close();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return serStr;
<span style="white-space:pre"> </span>}
}