2012-04-27 24 views
0

我工作的這一點,得到這個錯誤不能把我的手指上的錯誤:錯誤:java.io.InvalidClassException

這是我的類:

package stdaccmdmngmt; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.Date; 
import java.util.HashSet; 
import java.util.Set; 
import javax.swing.JOptionPane; 

/** 
* 
* @author princess 
*/ 
public class Student1 implements Serializable { 

public static Set<Student1> listOfStudents = new HashSet<Student1>();  
public static File outFile = new File("StudentRegistry.dat"); 

// Data memebers 
private String studentID; 
private String name; 
private Date dateOfReg; 

//Constructor 
Student1(String number,String name) 
{ 
this.studentID = number; 
this.name = name; 
dateOfReg = new Date(); 
} 


public String getName() 
{ 
return name; 
} 

public String getStudentID() 
{ 
return studentID; 
} 

public Date getDateOfReg() 
{ 
return dateOfReg; 
} 

public void register() throws FileNotFoundException, IOException, ClassNotFoundException  
{ 
Set<Student1> sss = new HashSet<Student1>();  
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outFile)); 
oos.writeObject(sss); 
oos.close();  
} 

public static HashSet<Student> getListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException 
{ 
HashSet ss; 
File inFile = new File("StudentRegistry.dat"); 
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(inFile)); 
Set<Student> aNewSet = (HashSet<Student>) ois.readObject(); 
return (HashSet<Student>) aNewSet; 
} 

public static Student getStudentbyID(String a) throws FileNotFoundException, IOException, ClassNotFoundException 
{ 
Set<Student> aNewSet = new HashSet<Student>(); 
aNewSet = Student.getListOfStudents(); 
Student result = null; 

for (Student x :aNewSet) 
{ 
     if (x.getStudentID().equalsIgnoreCase(a)) 
     { 
      result = x; 
      break; 
     } 
    } 
    if (result == null) 
    { 
     JOptionPane.showMessageDialog(null, "Apartment not found"); 
    } 
    return result;  

} 

    @Override 
public String toString() 
{ 
// include the code to retrieve assigned apartment  
return "Name: " + name +" StudentID: "+ studentID + " Registered On: " + dateOfReg; 
} 


    @Override 
public boolean equals(Object another) 
{ 
Student1 stud = (Student1)another; 
return this.name.equals(stud.name)&& this.studentID.equals(stud.studentID); 
} 

    @Override 
public int hashCode() 
{ 
int hash = name.hashCode(); 
return hash; 

}  
} 

,這是測試類:

package stdaccmdmngmt; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.HashSet; 
import java.util.Set; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author princess 
*/ 
public class Test { 

public static void main (String[]args) throws FileNotFoundException, ClassNotFoundException, IOException 
{   
Student1 s = new Student1("12345","Sasha"); 
     try { 
      s.register(); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 

Set<Student> nnn; 
nnn = Student.getListOfStudents(); 
for (Student x : nnn) 

    System.out.println(x.toString());  

} 

} 
* @author princess 
*/ 
public class Student1 implements Serializable { 

public static Set<Student1> listOfStudents = new HashSet<Student1>();  
public static File outFile = new File("StudentRegistry.dat"); 

// Data memebers 
private String studentID; 
private String name; 
private Date dateOfReg; 

//Constructor 
Student1(String number,String name) 
{ 
this.studentID = number; 
this.name = name; 
dateOfReg = new Date(); 
} 


public String getName() 
{ 
return name; 
} 

public String getStudentID() 
{ 
return studentID; 
} 

public Date getDateOfReg() 
{ 
return dateOfReg; 
} 

public void register() throws FileNotFoundException, IOException, ClassNotFoundException  
{ 
Set<Student1> sss = new HashSet<Student1>();  
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outFile)); 
oos.writeObject(sss); 
oos.close();  
} 

public static HashSet<Student> getListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException 
{ 
HashSet ss; 
File inFile = new File("StudentRegistry.dat"); 
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(inFile)); 
Set<Student> aNewSet = (HashSet<Student>) ois.readObject(); 
return (HashSet<Student>) aNewSet; 
} 

public static Student getStudentbyID(String a) throws FileNotFoundException, IOException, ClassNotFoundException 
{ 
Set<Student> aNewSet = new HashSet<Student>(); 
aNewSet = Student.getListOfStudents(); 
Student result = null; 

for (Student x :aNewSet) 
{ 
     if (x.getStudentID().equalsIgnoreCase(a)) 
     { 
      result = x; 
      break; 
     } 
    } 
    if (result == null) 
    { 
     JOptionPane.showMessageDialog(null, "Apartment not found"); 
    } 
    return result;  

} 

    @Override 
public String toString() 
{ 
// include the code to retrieve assigned apartment  
return "Name: " + name +" StudentID: "+ studentID + " Registered On: " + dateOfReg; 
} 


    @Override 
public boolean equals(Object another) 
{ 
Student1 stud = (Student1)another; 
return this.name.equals(stud.name)&& this.studentID.equals(stud.studentID); 
} 

    @Override 
public int hashCode() 
{ 
int hash = name.hashCode(); 
return hash; 

}  
} 

...........................

,我得到這個錯誤:

run: 
Exception in thread "main" java.io.InvalidClassException: stdaccmdmngmt.Student; local class incompatible: stream classdesc serialVersionUID = -3040096452457271695, local class serialVersionUID = -4849175799723926283 
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:562) 
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583) 
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) 
    at java.util.HashSet.readObject(HashSet.java:291) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) 
    at stdaccmdmngmt.Student.getListOfStudents(Student.java:76) 
    at stdaccmdmngmt.Test.main(Test.java:34) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 3 seconds) 

任何幫助?

感謝提前:)

回答

0

從的Javadoc摘自:

http://java.sun.com/j2se/1.4.2/docs/api/

公共類InvalidClassException 擴展ObjectStreamException

當Serialization運行時檢測到以下問題 一個拋出該異常一類。

  • 類的串行版本不匹配,從流中讀取類 描述的
  • 類包含未知數據類型
  • 類沒有可訪問的無參數的構造函數
+0

能否請您詳細一點對此的解釋是如何幫助OP? – Garbage 2012-04-27 04:43:52

+0

在這裏不是幫助OP變得更加自給自足的座右銘嗎? – Satya 2012-04-27 04:58:18

2

你遇到過使用Java序列化的一個缺陷是持久性框架(而不是通信的數據格式)。你堅持你的學生數據,然後改變學生班。現在,當您嘗試讀取持久數據時,它會失敗,因爲基本上,這兩個版本的類不匹配。如果您在學生類中手動指定了serialVersionUID,則序列化框架在讀取數據文件時不會窒息(不是馬上就是)。

但是根本問題是序列化與持久性不一樣 - 如果你想保存你的學生數據,那麼你需要選擇一個框架來避免類文件不兼容。如果項目複雜性有保證,我會建議將其保存爲CSV文件以開始查看,並查看數據庫解決方案。


*編輯*

作爲一個臨時的解決辦法,你可以嘗試重命名類回Student並設置serialVersionUID什麼序列化框架期待。這不是一個garaunteed修復,這取決於類有多少,因爲它改變了你創建的文件:

public class Student implements Serializable { 
    public static final long serialVersionUID = -3040096452457271695L; 

    // ... 
} 

由於其要求的項目中使用對象輸出流,我會建議實施Externalizable接口您Student類,貫徹的方法來讀取/寫入到一個對象輸出流:

public class Student implements Serializable, Externalizable { 
    public static final long serialVersionUID = -3040096452457271695L; 
    // ... 

    @Override 
    public void writeExternal(ObjectOutput out) throws IOException { 
     out.writeUTF(getStudentID()); 
     out.writeUTF(getName()); 
     // etc 
    } 

    @Override 
    public void readExternal(ObjectInput in) throws IOException, 
     ClassNotFoundException { 
     setStudentID(in.readUTF()); 
     setName(in.readUTF()); 
     // etc 
    } 

    // ... 
} 
+0

我會去一個數據庫解決方案,但根據我的要求(雅它的任務!)我必須使用ioObjectStream。任何簡單的方法來解決這個錯誤? – Sasha 2012-04-27 04:48:18

+0

查看編輯我的答案。 – Perception 2012-04-27 05:05:15

+0

我在Netbeans中打開了一個新項目並複製了EXACT代碼。 (畢竟只有一堂課)。無論如何。現在不知何故新文件沒有創建,它給了我以下錯誤:pr 27,2012 9:55:01 am hostelmanagement.HostelManagement main SEVERE:null java.io.FileNotFoundException:Student.dat(系統找不到文件指定) \t at java.io.FileInputStream.open(Native Method) \t at java.io.FileInputStream。 (FileInputStream.java:106) \t在hostelmanagement.Student.getListOfStudents(Student.java:86) – Sasha 2012-04-27 07:01:34