12

mac2025-04-10  4

12 自定义一个对象使用ObjectInputstream和ObjectOutputStream存储到硬盘上并读取

//需实现Serializable public class Student implements Serializable {} //将对象序列化到本地(磁盘) Student str = new Student("张三",35); ObjectOutputStream object=null; try { //创建ObjectOutputStream对象 object = new ObjectOutputStream(new FileOutputStream("e:\\测试类文件\\Student.txt")); object.writeObject(str); object.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ try { object.close(); } catch (IOException e) { e.printStackTrace(); } } //对象的反序列化 ObjectInputStream In=null; try{ //创建ObjectInputStream对象 In = new ObjectInputStream(new FileInputStream("e:\\测试类文件\\Student.txt")); Student St =(Student)In.readObject(); System.out.println(St); }catch (Exception e) { e.printStackTrace(); }finally{ try { In.close(); } catch (IOException e) { e.printStackTrace(); } }
最新回复(0)