天天看點

關于java對象流的練習

今天課上老師将關于java對象流的課程ObjectInputStream和ObjectOutputStream,我一直在想事情沒有注意聽是以錯過了些東西,下課前老師布置了關于java對象流的作業。我利用晚上的一些時間學習了一下順便把作業給KO了。

下面是關于本次小程式的介紹

Student->持久化類 

SaveStudent->将對象存儲到磁盤上,檔案名自己定,不一定要以.txt | .doc結尾。

ReadStudent->将儲存在磁盤上的資料讀入到控制台中。

ObjectIoTest->主要的測試

注意點:隻有實作了Serializable和Externalizable接口的類的對象才能被序列化。Externalizable接口繼承自Serializable接口,實作Externalizable接口的類完全由自身來控制序列化的行為,而僅實作Serializable接口的類可以采用預設的序列化方式 ,于是要在

Student類中導入:import java.io.Serializable;(有一個BUG,我已在評論中說明了,我這邊終端怎麼不好修改?)

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 }      

Student

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 }      

SaveStudent

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 }      

ReadStudent

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 }      

ObjectIoTest

轉載于:https://www.cnblogs.com/struCoder/p/3434342.html