让我清楚你在serialVersionUID
向文件写入和读取对象时的使用。
在下面的代码中,我编写了两个函数writeObject()
和readObj()
writeObject()
用于将对象写入文件
readObj()
用于从文件中读取对象
package com.msq;
import java.io.Serializable;
public class A implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
int a;
transient int b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
package com.msq;
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;
public class B implements Serializable {
/**
*
*/
private static final long serialVersionUID = 123L ;
/**
*
*/
String name;
A a;
public B() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public static void main(String[] args) {
//writeObject();
readObj();
}
static void writeObject() {
B b = new B();
b.setName("Musaddique");
A a2 = new A();
a2.setA(5);
a2.setB(10);
b.setA(a2);
ObjectOutputStream write = null;
try {
write = new ObjectOutputStream(new FileOutputStream(
"D:\\serObj.bat"));
write.writeObject(b);
write.flush();
write.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void readObj() {
ObjectInputStream reader = null;
try {
reader = new ObjectInputStream(
new FileInputStream("D:\\serObj.bat"));
B b1 = (B) reader.readObject();
System.out.println("name: "+b1.getName());
System.out.println("value of a: "+b1.getA().getA());
System.out.println("value of b: "+b1.getA().getB());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在这里,我使用serialVersionUID = 123L
了 BserialVersionUID = 1L
类和 A 类,还使用transient
了变量 b 的关键字,以限制将 b 的值保存到文件中。
1)将数据写入文件然后读取文件,您将获得以下输出
name: Musaddique
value of a: 5
value of b: 0
你会得到 b: 0 的值,因为我们对 b 使用了瞬态。
现在进行测试尝试通过相同的调用写入对象,但是在读取更改时,您serialVersionUID = 765L
将得到以下异常
java.io.InvalidClassException: com.msq.B; local class incompatible: stream classdesc serialVersionUID = 123, local class serialVersionUID = 765
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.msq.B.readObj(B.java:81)
at com.msq.B.main(B.java:46)
serialVersionUID
因此,在从文件中读取和写入对象时必须使用相同的方法。
此外,当您将类从一台机器合并到另一台机器或系统时,它在 RMI 调用中使用。