Java序列化和反序列化


/**

* 序列化

* @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;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章