import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class UpdatePath implements Serializable { private final String path; public UpdatePath(String path) { this.path = path; } public static void main(String[] arg) throws Exception { ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteoutStream); out.writeObject(new UpdatePath("124kkkk")); out.flush(); final byte[] bytes = byteoutStream.toByteArray(); System.out.println(bytes); for (int i = 0; i < bytes.length; i++) { System.out.print(bytes[i]); System.out.print(" "); } out.close(); ObjectInputStream messageReader = new ObjectInputStream( new ByteArrayInputStream(bytes)); UpdatePath path = (UpdatePath) messageReader.readObject(); System.out.println("============================="); System.out.println(path.path); messageReader.close(); } }
java对象序列化序列化例子
最新推荐文章于 2021-02-28 13:36:53 发布
本文介绍了一个简单的Java程序,演示了如何使用Java内置的序列化接口将对象转换为字节数组,以及如何从字节数组中还原对象。此示例涉及的类实现了Serializable接口,并通过ObjectOutputStream和ObjectInputStream完成对象的状态保存和恢复。
227

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



