2012-10-18 63 views
29

編寫了下面的代碼之後,我現在必須在StudentData中使用自定義的readObject()和writeObject()重寫方法來讀取和寫入對象的變量。不使用defaultWriteObject或defaultReadObject方法來執行此操作。序列化 - readObject writeObject覆蓋

問題是,我不完全理解被要求做什麼。我已閱讀Uses of readObject/writeObject in Serialization,但我無法理解它。有人指着我朝着正確的方向走?

我的代碼:

import java.io.*; //importing input-output files 

class Student implements java.io.Serializable { 

    String name; // declaration of variables 
    String DOB; 
    int id; 

    Student(String naam, int idno, String dob) // Initialising variables to user 
               // data 
    { 
     name = naam; 
     id = idno; 
     DOB = dob; 
    } 

    public String toString() { 
     return name + "\t" + id + "\t" + DOB + "\t"; 
    } 

} 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 

class StudentData      //main class 
{ 
    public static void main(String args[]) throws IOException     //exception handling 
    { 
     System.out.println("Enter the numbers of students:"); 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     int n = Integer.parseInt(in.readLine()); 

     Student[] students = new Student[n]; 

     //Student[] S=new Student[n];      // array of objects declared and defined 
     for (int i = 0; i < students.length; i++) { 
      System.out.println("Enter the Details of Student no: " + (i + 1));    //reading data form the user 
      System.out.println("Name: "); 
      String naam = in.readLine(); 
      System.out.println("ID no: "); 
      int idno = Integer.parseInt(in.readLine()); 
      System.out.println("DOB: ");    
      String dob = (in.readLine()); 

      students[i] = new Student(naam, idno, dob);       

      File studentFile = new File("StudentData.txt"); 
      try { 
       FileOutputStream fileOutput = new FileOutputStream(studentFile); 
       ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); 
       objectOutput.writeObject(students); 

       students = null; 

       FileInputStream fileInput = new FileInputStream(studentFile); 
       ObjectInputStream objectInputStream = new ObjectInputStream(fileInput); 

       students = (Student[]) objectInputStream.readObject(); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 

      for (Student student : students) { 
       System.out.println(student); 
      } 
     } 
    } 
} 
+0

奇怪的分配。你應該*調用defaultWriteObject()和defaultReadObject()。 – EJP

回答

49

你要做這樣的:只是創造學生的實例後

import java.io.IOException; 

class Student implements java.io.Serializable { 

    String name; 
    String DOB; 
    int id; 

    Student(String naam, int idno, String dob) { 
     name = naam; 
     id = idno; 
     DOB = dob; 
    } 

    private void writeObject(java.io.ObjectOutputStream stream) 
      throws IOException { 
     stream.writeObject(name); 
     stream.writeInt(id); 
     stream.writeObject(DOB); 
    } 

    private void readObject(java.io.ObjectInputStream stream) 
      throws IOException, ClassNotFoundException { 
     name = (String) stream.readObject(); 
     id = stream.readInt(); 
     DOB = (String) stream.readObject(); 
    } 

    public String toString() { 
     return name + "\t" + id + "\t" + DOB + "\t"; 
    } 

} 

利用readObject調用(繞過正常的構造)。

+7

只是在那裏指出一個細節。命令或寫/讀**是重要的。在這個例子中,寫作是:name,id,DOB,所以當你讀到 – Manu

+3

時,你會得到名字,ID,DOB它會在沒有空構造函數的情況下工作嗎? – borjab

相關問題