公司动态

Java-实例-序列化与反序列化(Serializable)

📅 2026/8/14 23:21:15
Java-实例-序列化与反序列化(Serializable)
理解Serializable序列化---将对象状态转换为字节流以便存储或传输。序列化是冻结对象反序列化是解冻对象想让一个对象可序列化其类必须实现java.io.Serializable接口。这个接口没有任何方法只起标记作用告诉JVM这个类的对象允许被序列化。在Android中进行序列化主要有两种选择Java原生的Serializable和Android专属的Parcelable。它们最核心的区别在于设计初衷和应用场景Serializable是Java的标准接口使用简单但效率较低适合持久化存储存文件或网络传输-2-8-11。Parcelable是Android特有的接口实现稍复杂但性能极高专为内存间的高效数据传输而设计是Android组件间通信如Intent、Binder跨进程通信的首选实例将对象person转换为字节流存储在工程target文件夹中。调用Person person new Person(九股烟, 20); byte[] bytes0 serialize(person); // 序列化将对象保存到文件 save(bytes0); //保存 byte[] bytes1 read(); //读出 Person p deserialize(bytes1); // 反序列化从文件恢复对象 System.out.println(反序列化对象 p.toString());功能函数// // 1. 序列化对象 → 字节数组 // public static byte[] serialize(Person person) { try ( ByteArrayOutputStream baos new ByteArrayOutputStream(); ObjectOutputStream oos new ObjectOutputStream(baos)) { oos.writeObject(person); oos.flush(); return baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } } // // 2. 反序列化字节数组 → 对象 // public static Person deserialize(byte[] bytes) { if (bytes null || bytes.length 0) {return null;} try (ByteArrayInputStream bais new ByteArrayInputStream(bytes); ObjectInputStream ois new ObjectInputStream(bais)) { return (Person) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); return null; } } String targetDir target/; String fileName path_bytes.km; // // 3. 保存字节数组 → 文件 // public void save(byte[] data) { try { // 确保target目录存在 File file new File(targetDir, fileName); file.getParentFile().mkdirs();// 确保target目录存在 try (FileOutputStream fos new FileOutputStream(file)) { fos.write(data); System.out.println(保存成功: file.getAbsolutePath()); System.out.println(数据大小: data.length 字节); } } catch (IOException e) { e.printStackTrace(); } } // // 4. 读取文件 → 字节数组 // public byte[] read() { File file new File(targetDir, fileName); file.getParentFile().mkdirs();// 确保target目录存在 if (!file.exists()) {return null;} try (FileInputStream fis new FileInputStream(file)) { byte[] data new byte[(int) file.length()]; fis.read(data); System.out.println(读取成功: file.getAbsolutePath()); System.out.println(数据大小: data.length 字节); return data; } catch (IOException e) { e.printStackTrace(); return null; } }Person.java 接口Serializableimport java.io.Serializable; public class Person implements Serializable{ private String name; private int age; public Person(String name, int age) { this.name name; this.age age; } public String getName() { return name; } public void setName(String name) { this.name name; } public int getAge() { return age; } public void setAge(int age) { this.age age; } Override public String toString() { return Person{ name name \ , age age }; } }