Java序列化和反序列化

独自空忆成欢 提交于 2020-01-02 00:19:59

 

/**

* 序列化

* @throws Exception

*/

public String OutputStream(List<?> results) throws Exception {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

ObjectOutputStream objectOutputStream = new ObjectOutputStream(

byteArrayOutputStream);

objectOutputStream.writeObject(results);

String serStr = byteArrayOutputStream.toString("ISO-8859-1");

serStr = java.net.URLEncoder.encode(serStr, "UTF-8");

objectOutputStream.close();

byteArrayOutputStream.close();

System.out.println(serStr);

return serStr;

}

 

/**

* 反序列化

* @throws Exception

*/

public List<?> InputStream(String serStr) throws Exception {

String redStr = java.net.URLDecoder.decode(serStr, "UTF-8");

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(

redStr.getBytes("ISO-8859-1"));

ObjectInputStream objectInputStream = new ObjectInputStream(

byteArrayInputStream);

List<?> results = (List<?>) objectInputStream.readObject();

objectInputStream.close();

byteArrayInputStream.close();

return results;

}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!