List<Date> list=new ArrayList<Date>();
Date now=new Date();
list.add(now);
list.add(new Date(now.getTime()+8888888));
ByteArrayOutputStream byteStream=new ByteArrayOutputStream();
ObjectOutput output=new ObjectOutputStream(byteStream);
output.writeObject(list);
byte b[]=byteStream.toByteArray();
BASE64Encoder base64Encoder=new BASE64Encoder();
String cookieString=base64Encoder.encode(b);
System.out.println("经过BASE64编码的Cookie:\n"+cookieString);
System.out.println("\n开始解码");
BASE64Decoder base64Decoder=new BASE64Decoder();
byte buf[]=base64Decoder.decodeBuffer(cookieString);
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buf);
ObjectInput input=new ObjectInputStream(byteArrayInputStream);
List<Date> afterList=(List<Date>) input.readObject();
for (Date date : afterList) {System.out.println(date);}
ArrayList 的序列化
最新推荐文章于 2025-08-31 15:11:19 发布
本文介绍了一种使用Java实现日期列表的序列化与反序列化的具体方法,包括利用ArrayList存储Date对象、通过ObjectOutputStream进行序列化处理,并借助BASE64编码将字节数组转换为字符串形式以便于存储和传输;同时也展示了如何反序列化该字符串以恢复原始的日期列表。
899

被折叠的 条评论
为什么被折叠?



