Java ObjectOutputStream ObjectInputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package ersatz; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Ersatz { public static void main(String[] args) { String file = "d:/data.dat" ; ObjectOutputStream oos= null ; try { oos = new ObjectOutputStream( new FileOutputStream(file)); oos.writeInt(100); // int -> Integer Integer implements Serializable oos.writeBoolean( true ); // boolean -> Boolean Serializable oos.writeChar( 'A' ); // char -> Character oos.writeDouble(9.9); // double -> Double oos.writeUTF( "范德萨" ); oos.writeObject( new Dog( "匹配" ,22)); // preserve dog object System. out .println( "done" ); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } class Dog{ private String name; private int age; public Dog(String name, int age) { this .name = name; this .age = age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package ersatz.yond; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Yond { public static void main(String[] args) throws IOException, ClassNotFoundException { String file = "d:/data.dat" ; // 读取(反序列化)的顺序需和序列化的顺序一致 ObjectInputStream ois = new ObjectInputStream( new FileInputStream(file)); System. out .println(ois.readInt()); System. out .println(ois.readBoolean()); System. out .println(ois.readChar()); System. out .println(ois.readDouble()); System. out .println(ois.readUTF()); // dog编译类型Object,运行类型Dog Object dog = ois.readObject(); System. out .println( "run type: " +dog.getClass()); System. out .println( "dog: " +dog); } } |
重写 Dog toString
需重新生成文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package ersatz.yond; import ersatz.Dog; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Yond { public static void main(String[] args) throws IOException, ClassNotFoundException { String file = "d:/data.dat" ; // 读取(反序列化)的顺序需和序列化的顺序一致 ObjectInputStream ois = new ObjectInputStream( new FileInputStream(file)); System. out .println(ois.readInt()); System. out .println(ois.readBoolean()); System. out .println(ois.readChar()); System. out .println(ois.readDouble()); System. out .println(ois.readUTF()); // dog编译类型Object,运行类型Dog Object dog = ois.readObject(); System. out .println( "run type: " +dog.getClass()); System. out .println( "dog: " +dog); // 若要调用Dog方法,需向下转型, 并把Dog类的定义,放在可以引用的位置 Dog p=(Dog) dog; System. out .println(p.getName()); // 关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流 ois.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package ersatz; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Ersatz { public static void main(String[] args) { String file = "d:/data.dat" ; ObjectOutputStream oos = null ; try { oos = new ObjectOutputStream( new FileOutputStream(file)); oos.writeInt(100); // int -> Integer Integer implements Serializable oos.writeBoolean( true ); // boolean -> Boolean Serializable oos.writeChar( 'A' ); // char -> Character oos.writeDouble(9.9); // double -> Double oos.writeUTF( "范德萨" ); oos.writeObject( new Dog( "匹配" , 22, "nation" , "pink" )); // preserve dog object System. out .println( "done" ); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package ersatz; import java.io.Serializable; public class Dog implements Serializable { // serialVersionUID 序列化版本号 private static final long serialVersionUID = 1; // 序列化对象时,默认把除了static,transient修饰的所有属性序列化 private static String nation; private String name; private int age; private final transient String color; // 序列化对象时,属性的类型亦需实现 Serializable 接口 private final Master master = new Master(); public Dog(String name, int age, String nation, String color) { this .name = name; this .age = age; Dog.nation = nation; this .color = color; } 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 "Dog{" + "name='" + name + '\ '' + "color='" + color + '\ '' + ", age=" + age + '}' + nation + " " + master; } } |
1 2 3 4 5 6 | package ersatz; import java.io.Serializable; public class Master implements Serializable { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package ersatz.yond; import ersatz.Dog; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Yond { public static void main(String[] args) throws IOException, ClassNotFoundException { String file = "d:/data.dat" ; // 读取(反序列化)的顺序需和序列化的顺序一致 ObjectInputStream ois = new ObjectInputStream( new FileInputStream(file)); System. out .println(ois.readInt()); System. out .println(ois.readBoolean()); System. out .println(ois.readChar()); System. out .println(ois.readDouble()); System. out .println(ois.readUTF()); // dog编译类型Object,运行类型Dog Object dog = ois.readObject(); System. out .println( "run type: " + dog.getClass()); System. out .println( "dog: " + dog); // 若要调用Dog方法,需向下转型, 并把Dog类的定义,放在可以引用的位置 Dog p = (Dog) dog; System. out .println(p.getName()); // 关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流 ois.close(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律