ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ package io.a8; import java.io.*; public class Test { public static void main(String[] args) { ObjectOutputStream osc = null; ObjectInputStream osr = null; Student ss = new Student("悟空", 007, 95.5f); Student ss2 = null; try { FileOutputStream wsc = new FileOutputStream("e:/ccc.txt"); osc = new ObjectOutputStream(wsc); osc.writeObject(ss); osr = new ObjectInputStream(new FileInputStream("e:/ccc.txt")); ss2 = (Student)osr.readObject(); //readObject()用于读取数据,并且要进行类型转换 System.out.println("姓名:"+ss2.xingming); System.out.println("学号:"+ss2.xuehao); System.out.println("成绩:"+ss2.chengji); } catch (Exception e) { System.out.println("出现错误"); } finally//finally的作用是善后 { try { osc.close(); osr.close(); System.exit(-1); } catch (Exception e) { System.exit(-1); } }//finally下面的try..catch都要写上起到退出作用的话 }//如果try..catch的后面有finally,那么try..catch不要写退出程序的代码 } class Student implements Serializable //接口是空的,起到标准作用 { String xingming = null; int xuehao = 0; transient float chengji = 0; //transient的作用是忽略,外界传不过来数据 public Student(String xingming, int xuehao, float chengji) { this.xingming=xingming; this.xuehao=xuehao; this.chengji=chengji; } } ~~~ ![](https://box.kancloud.cn/fbffc095b4822b4c7b1a35884904f0fc_196x113.png) 并且e盘中ccc.txt文件显示 ![](https://box.kancloud.cn/c2bb53612cf0f37c87ab4080a0483538_744x89.png)