package com.Lucky.io.serializeOrUnserialize; import java.io.*; /* 序列化流: 将java对象写到本地文件中【应用场景:游戏存档……………………】 反序列化流: 读取本地文件中java对象 */ public class serializeDemo { public static void main(String[] args) throws IOException { //创建序列化流【对象操作输出流】 ObjectOutputStream outputStream= new ObjectOutputStream( new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\serializeOrUnserialize\\ObjectTest.txt")); //创建要储存的对象 student stu=new student("唯易",22); outputStream.writeObject(stu); outputStream.close(); } }
package com.Lucky.io.serializeOrUnserialize; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; public class unSerializeDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { //////////////////////////////反序列化/////////////////////////////////////////////////// ObjectInputStream oiStream= new ObjectInputStream( new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\serializeOrUnserialize\\ObjectTest.txt")); Object obj = oiStream.readObject(); System.out.println(obj); student nn= (student)obj; System.out.println(nn); oiStream.close(); } }
标签:java,student,流之,io,new,import,序列化 From: https://www.cnblogs.com/Lucky-only/p/16970863.html