今天课上老师将关于java对象流的课程ObjectInputStream和ObjectOutputStream,我一直在想事情没有注意听所以错过了些东西,下课前老师布置了关于java对象流的作业。我利用晚上的一些时间学习了一下顺便把作业给KO了。
下面是关于本次小程序的介绍
Student->持久化类
SaveStudent->将对象存储到磁盘上,文件名自己定,不一定要以.txt | .doc结尾。
ReadStudent->将保存在磁盘上的数据读入到控制台中。
ObjectIoTest->主要的测试
注意点:只有实现了Serializable和Externalizable接口的类的对象才能被序列化。Externalizable接口继承自Serializable接口,实现Externalizable接口的类完全由自身来控制序列化的行为,而仅实现Serializable接口的类可以采用默认的序列化方式 ,于是要在
Student类中导入:import java.io.Serializable;(有一个BUG,我已在评论中说明了,我这边终端怎么不好修改?)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package objectIO; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable{ 6 /** 7 * 8 */ 9 private static final long serialVersionUID = 1L; 10 private String stuName; 11 private int stuAge; 12 private String stuClass; 13 public String getStuName() { 14 return stuName; 15 } 16 public void setStuName(String stuName) { 17 this.stuName = stuName; 18 } 19 public int getStuAge() { 20 return stuAge; 21 } 22 public void setStuAge(int stuAge) { 23 this.stuAge = stuAge; 24 } 25 public String getStuClass() { 26 return stuClass; 27 } 28 public void setStuClass(String stuClass) { 29 this.stuClass = stuClass; 30 } 31 32 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package objectIO; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.ObjectOutputStream; 8 import java.io.Serializable; 9 10 public class SaveStudent implements Serializable { 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1L; 15 public void SaveToFile(File file,Student[] stu){ 16 try { 17 FileOutputStream fos = new FileOutputStream(file); 18 ObjectOutputStream oos = new ObjectOutputStream(fos); 19 oos.writeObject(stu); 20 System.out.println("-----------成功导入到文件-------------"); 21 oos.flush(); 22 oos.close(); 23 } catch (FileNotFoundException e) { 24 e.printStackTrace(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package objectIO; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.ObjectInputStream; 8 9 public class ReadStudent { 10 11 public void readFromFIle(File file){ 12 Student[] stu = new Student[5]; 13 try { 14 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); 15 stu =(Student[]) ois.readObject(); 16 for(int i = 0;i<stu.length;i++){ 17 System.out.println("name:"+stu[i].getStuName()+" age:"+stu[i].getStuAge()+" Class:"+stu[i].getStuClass()); 18 } 19 ois.close(); 20 System.out.println("--------------共导入了"+stu.length+"条数据------------\n---------------成功读入文件--------------"); 21 } catch (FileNotFoundException e) { 22 e.printStackTrace(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } catch (ClassNotFoundException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 30 } 31 32 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package objectIO; 2 3 import java.io.File; 4 import java.util.Scanner; 5 6 public class ObjectIoTest { 7 8 public static void main(String[] args) { 9 //new ObjectIoTest().WriteObject(); //此处执行向文件的写入操作 10 //此处执行向控制台中读入操作 11 File file = new File("D:/MyDemo.DZ"); 12 new ReadStudent().readFromFIle(file); 13 14 } 15 public void WriteObject(){ 16 Student myStudent = new Student(); 17 System.out.print("请输入需要存储的学生的个数:"); 18 Scanner sc = new Scanner(System.in); 19 int n = sc.nextInt(); 20 Student [] stu = new Student[n]; 21 System.out.println("==========下面程序开始接受输入=========="); 22 for(int i = 0;i<stu.length;i++){ 23 System.out.print("请输入第"+(i+1)+"个学生的姓名:"); 24 String stuName = sc.next(); 25 myStudent.setStuName(stuName); 26 System.out.print("请输入第"+(i+1)+"个学生的年龄:"); 27 int stuAge = sc.nextInt(); 28 myStudent.setStuAge(stuAge); 29 System.out.print("请输入第"+(i+1)+"个学生的班级:"); 30 String stuClass = sc.next(); 31 myStudent.setStuClass(stuClass); 32 stu[i] = myStudent; 33 System.out.println("------------------------------------"); 34 } 35 System.out.println("您一共输入了"+stu.length+"个学生"); 36 File myFile = new File("D:/MyDemo.DZ"); 37 new SaveStudent().SaveToFile(myFile, stu); 38 } 39 40 }