java序列化與反序列化工具
阿新 • • 發佈:2019-02-15
package com.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; /** * @作者 王建明 * @建立日期 2012-10-12 * @建立時間 上午10:48:41 * @版本號 V 1.0 */ public class ObjectSerializeUtil { /** * @param args * @throws IOException * @throws ClassNotFoundException * @作者 王建明 * @建立日期 2012-10-12 * @建立時間 上午10:48:41 * @描述 —— */ public static void main(String[] args) throws IOException, ClassNotFoundException { Customer customer = new Customer("阿蜜果", 24); String serStr = getStrFromObj(customer); customer = (Customer) getObjFromStr(serStr); System.out.println(customer); } /** * @param serStr * @throws UnsupportedEncodingException * @throws IOException * @throws ClassNotFoundException * @作者 王建明 * @建立日期 2012-10-12 * @建立時間 上午10:56:27 * @描述 —— 將字串反序列化成物件 */ public static Object getObjFromStr(String serStr) throws UnsupportedEncodingException, IOException, ClassNotFoundException { String redStr = java.net.URLDecoder.decode(serStr, "UTF-8"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( redStr.getBytes("ISO-8859-1")); ObjectInputStream objectInputStream = new ObjectInputStream( byteArrayInputStream); Object result = objectInputStream.readObject(); objectInputStream.close(); byteArrayInputStream.close(); return result; } /** * @return * @throws IOException * @throws UnsupportedEncodingException * @作者 王建明 * @建立日期 2012-10-12 * @建立時間 上午10:51:07 * @描述 —— 將物件序列化成字串 */ public static String getStrFromObj(Object obj) throws IOException, UnsupportedEncodingException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( byteArrayOutputStream); objectOutputStream.writeObject(obj); String serStr = byteArrayOutputStream.toString("ISO-8859-1"); serStr = java.net.URLEncoder.encode(serStr, "UTF-8"); objectOutputStream.close(); byteArrayOutputStream.close(); return serStr; } } /** * @作者 王建明 * @建立日期 2012-10-12 * @建立時間 上午10:59:07 * @描述 —— 測試物件封裝 */ class Customer implements Serializable { private String name; private int age; public Customer(String name, int age) { this.name = name; this.age = age; } public String toString() { return "name=" + name + ", age=" + age; } }