2014-02-18 92 views
1

我有這樣的代碼寫入對象:的Java序列化問題

FileOutputStream fileOut = new FileOutputStream("Model.ser"); 
ObjectOutputStream out = new ObjectOutputStream(fileOut); 
out.writeObject(this); 
out.close(); 
fileOut.close(); 

而這種代碼加載的對象:

Model m = null; 
try { 
    FileInputStream fileIn = new FileInputStream("Model.ser"); 
    ObjectInputStream in = new ObjectInputStream(fileIn); 
    m = (Model) in.readObject(); 
    in.close(); 
    fileIn.close(); 
} catch (Exception e) {} 

setStudentList(m.getStudentList()); 
setModuleList(m.getModuleList()); 

我敢肯定,節約工程,爲我打開時記事本+ + +中的文件,我看到了我保存的大部分數據,但是當我加載時,模塊列表中沒有數據。

完整的源代碼:

import java.io.*; 
import java.util.*; 

public class Model implements java.io.Serializable { 

private Student[] studentList = new Student[0]; 
private Module[] moduleList = new Module[0]; 

public void menu() { 
    while (true) { 
     System.out.println ("MENU"); 
     System.out.println (""); 
     System.out.println (" 1 - Run Tests"); 
     System.out.println (" 2 - Add Student"); 
     System.out.println (" 3 - Add Module"); 
     System.out.println (" 4 - Add Student To Module"); 
     System.out.println (" 5 - Save System (Text file)"); 
     System.out.println (" 6 - Load System (Text file)"); 
     System.out.println (" 7 - Save System (Serialized)"); 
     System.out.println (" 8 - Load System (Serialized)"); 
     System.out.println (" 9 - Print Report"); 
     System.out.println (""); 
     System.out.print ("Enter choice: "); 

     String input = keyboard.readString(); 

     switch (input) { 
      case "1" : 
       runTests(); 
       break; 
      case "2" : 
       System.out.print("First Name : "); 
       String fN = keyboard.readString(); 
       System.out.print("Surname : "); 
       String sN = keyboard.readString(); 
       System.out.print("Course Code : "); 
       String c = keyboard.readString(); 
       System.out.print("User ID : "); 
       String iD = keyboard.readString(); 
       AddStudent(iD, sN, fN, c); 
       break; 

      case "3" : 
       System.out.print("Module Code : "); 
       String code = keyboard.readString(); 
       String[] temp = new String[0]; 
       AddModule(code,temp); 
       break; 
      case "4" : 
       System.out.print("Module Code : "); 
       code = keyboard.readString(); 
       Module m = findAModule(code); 
       if (m != null) { 
        System.out.print("User ID : "); 
        iD = keyboard.readString(); 
        Student s = findAStudent(iD); 
        if (s != null) { 
         m.addThisStudent(s); 
        } else { 
         System.out.println("Student not found"); 
        } 
       } else { 
        System.out.println("Module not found"); 
       } 
       break; 
      case "5" : 
       saveToTextFiles(); 
       break; 
      case "6" : 
       loadFromTextFiles(); 
       break; 
      case "7" : 
       saveSerialized(); 
       break; 
      case "8" : 

       break; 
      case "9" : 
       printReport(); 
       break;     
     } 
    } 
} 

public void runTests() { 
    loadFromTextFiles(); 
    saveSerialized(); 
    printReport(); 
} 

public void loadFromTextFiles() { 
    studentList = new Student[0]; 
    moduleList = new Module[0]; 
    try { 
     Scanner fileReader = new Scanner(new InputStreamReader(new FileInputStream("students.txt"))); 
     int num = fileReader.nextInt(); fileReader.nextLine(); 
     for (int i = 0; i < num; i++) { 
      String u = fileReader.nextLine(); 
      String sn = fileReader.nextLine(); 
      String fn = fileReader.nextLine(); 
      String c = fileReader.nextLine(); 
      AddStudent(u, sn, fn, c); 
     } 
     fileReader.close(); 

     fileReader = new Scanner(new InputStreamReader(new FileInputStream("modules.txt"))); 
     num = fileReader.nextInt(); fileReader.nextLine(); 
     for (int i = 0; i < num; i++) { 
      String code = fileReader.nextLine(); 
      int numOfStudents = fileReader.nextInt(); fileReader.nextLine(); 
      String[] students = new String[numOfStudents]; 
      for (int j = 0; j < numOfStudents; j++) { 
       students[j] = fileReader.nextLine(); 
      } 
      AddModule(code, students); 
     } 
     fileReader.close(); 
    } catch (IOException e) {} 

} 

public void saveToTextFiles() { 
    try { 
     PrintWriter outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("students.txt"))); 
     outfile.println(studentList.length); 
     for (int i = 0; i < studentList.length; i++) { 
      outfile.println(studentList[i].getUID()); 
      outfile.println(studentList[i].getSN()); 
      outfile.println(studentList[i].getFN()); 
      outfile.println(studentList[i].getDegree()); 
     } 
     outfile.close(); 

     outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("modules.txt"))); 
     outfile.println(moduleList.length); 
     for (int i = 0; i < moduleList.length; i++) { 
      outfile.println(moduleList[i].getCode()); 
      outfile.println(moduleList[i].getStudents().length); 
      for (int j = 0; j < moduleList[i].getStudents().length; j++) { 
       outfile.println(moduleList[i].getStudents()[j]); 
      } 
     } 
     outfile.close(); 
    } catch (IOException e) {} 
} 

public void saveSerialized() { 
    try { 
     FileOutputStream fileOut = new FileOutputStream("Model.ser"); 
     ObjectOutputStream out = new ObjectOutputStream(fileOut); 
     out.writeObject(this); 
     out.close(); 
     fileOut.close(); 

     FileOutputStream fileOut2 = new FileOutputStream("Module.ser"); 
     ObjectOutputStream out2 = new ObjectOutputStream(fileOut2); 
     out2.writeObject(studentList); 
     out2.close(); 
     fileOut2.close(); 

     FileOutputStream fileOut3 = new FileOutputStream("Student.ser"); 
     ObjectOutputStream out3 = new ObjectOutputStream(fileOut3); 
     out3.writeObject(moduleList); 
     out3.close(); 
     fileOut3.close(); 
    } catch (IOException e) {} 
} 

public void loadSerialized() { 
    Model m = null; 
    try { 
     FileInputStream fileIn = new FileInputStream("Model.ser"); 
     ObjectInputStream in = new ObjectInputStream(fileIn); 
     m = (Model) in.readObject(); 
     in.close(); 
     fileIn.close(); 
    } catch (Exception e) {} 

    setStudentList(m.getStudentList()); 
    setModuleList(m.getModuleList()); 
} 

private Module[] getModuleList() { 
    return moduleList; 
} 

private Student[] getStudentList() { 
    return studentList; 
} 

private void setModuleList(Module[] m) { 
    moduleList = m.clone(); 
} 

private void setStudentList(Student[] s) { 
    studentList = s.clone(); 
} 

private void AddModule(String code, String[] students) { 
    int length = moduleList.length; 
    Module NewArray[] = new Module[length + 1]; 
    for (int i = 0; i < length + 1; i++) { 
     if (i < length) { 
      NewArray[i] = new Module(moduleList[i]); 
     } 
    } 
    NewArray[length] = new Module(code, students); 
    moduleList = NewArray.clone(); 
} 

private void AddStudent(String u, String sn, String fn, String c) { 
    int length = studentList.length; 
    Student NewArray[] = new Student[length + 1]; 
    for(int i = 0; i < length + 1; i++) { 
     if (i < length) { 
      NewArray[i] = new Student(studentList[i]); 
     } 
    } 
    NewArray[length] = new Student(u, sn, fn, c); 
    studentList = NewArray.clone(); 
} 

public void printReport() { 
    for (int i = 0; i < moduleList.length; i++) { 
     System.out.println(moduleList[i].toString(this)); 
    } 
} 

public Student findAStudent(String uid) { 
    for (int i = 0; i < studentList.length; i++) { 
     if (studentList[i].getUID().compareTo(uid) == 0) { 
      return studentList[i]; 
     } 
    } 
    return null; 
} 

public Module findAModule(String code) { 
    for (int i = 0; i < moduleList.length; i++) { 
     if (moduleList[i].getCode().compareTo(code) == 0) { 
      return moduleList[i]; 
     } 
    } 
    return null; 
} 
} 
+0

郵完整的源代碼。 – RaviH

+0

請解釋一下什麼是「問題」是 –

+0

的問題,如果我保存使用一些數據'saveSerialized()'我不能取回數據以'loadSerialized()' –

回答

1

看看你的代碼示例,尤其是switch語句:

switch (input) { 

    ... 

    case "8" : 

     break; 

    ... 

} 

我會假設方法loadSerialized應該有所謂的,但它的失蹤,是沒有在代碼中的其他地方調用。

一旦您真的調用了執行加載的方法,假設您已爲StudentModule聲明瞭serialVersionUID,代碼將起作用。

編輯:爲什麼使用序列中持久對象是一個壞主意

簡單來說,使用序列中持久對象是易碎的。

對象的序列化表格與其類相關聯。類會隨着時間而改變,這意味着舊的序列化實例不能加載到新的實例中。

儘管您可以通過設置serialVersionUID來解決這個問題,但是這樣做會引入問題的反向,在引入新字段的情況下,新的對象不能讀入舊類的對象中,如果這樣做會成爲問題您會對部署的系統執行滾動更新。

還有其他原因主機 - 這不是容易閱讀(這意味着你可以不喜歡你更新它會在數據庫或XML/JSON文檔),這是效率低下等

Model類的
+0

FWIW,serialVersionUID不是絕對必要的。 serialVersionUID就像公共捍衛者一樣。如果你不提供一個,那麼你會提供一個,它可能不是你想要的。 – jsight

+1

是的,但提供的'serialVersionUID'在類被重新編譯時可能會有所不同,所以如果要使用序列化來持久化對象(這首先是一個壞主意),那麼最好是安全的並添加一個。 –

+0

哦哇,即時通訊不知道我錯過了,謝謝!我已經閱讀了關於使用序列化進行持久化的分配,爲什麼這是一件壞事? –