gpt4 book ai didi

Java序列化不同的Java版本

转载 作者:行者123 更新时间:2023-12-02 03:07:23 24 4
gpt4 key购买 nike

我有两个疑问。

i)这是我的主课。

 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;

@SuppressWarnings("javadoc")
public class Serializer {

public static final String FILE_NAME = "Person.ser";

public static void main(final String[] args) {
final Person person = new Person("Akash", 18, new Community("Hindu"));
try {
final File f = new File(FILE_NAME);
if (!f.isDirectory() && f.isFile()) {
System.out.println("File :: " + FILE_NAME
+ " has been deleted ? " + f.delete());
}

final ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream(FILE_NAME));
System.out.println("Writing object to file");
System.out.println(person);
os.writeObject(person);
os.close();

// Reading from file

final ObjectInputStream oins = new ObjectInputStream(
new FileInputStream(FILE_NAME));

System.out.println("Reading From File");
System.out.println(oins.readObject());

} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final ClassNotFoundException e) {
e.printStackTrace();
}
}
}

class Person implements Serializable {

String name;
int age;
transient private Community community;

public Person(final String name, final int age, final Community community) {
super();
this.name = name;
this.age = age;
this.community = community;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(final int age) {
this.age = age;
}

public Community getCommunity() {
return community;
}

public void setCommunity(final Community community) {
this.community = community;
}

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", community="
+ community + "]";
}

private void writeObject(final ObjectOutputStream oos) {
try {
oos.defaultWriteObject();
oos.writeObject(community.getName());
oos.writeObject(this.getAge());
oos.writeObject(this.getName());

} catch (final IOException e) {
e.printStackTrace();
}
}

private void readObject(final ObjectInputStream ois) {
try {
ois.defaultReadObject();
community = new Community((String) ois.readObject());
// person = new Person((String) ois.readObject(),
// (int) ois.readObject(), new Community(
// (String) ois.readObject()));
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}


class Community {

String name;

public Community(final String name) {
super();
this.name = name;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

@Override
public String toString() {
return "Community [name=" + name + "]";
}

}

我能够在 Java 7 中生成序列化文件。但是当我尝试在 Java 8 中读取同一文件时。它无法反序列化。我遇到以下异常。

    java.lang.ClassNotFoundException: Person
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:628)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1620)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at Serializer.main(Serializer.java:

有什么遗漏的吗?

2)我理解SUID的目的是唯一地标识一个类。它有助于(取消)编码事情,但如何(任何简单的例子将不胜感激)。我们也可以使用自己的算法来生成 SUID 以及如何使其跨 Java 版本兼容?

提前致谢!

最佳答案

1) 序列化过程不依赖于JDK版本,而是依赖于serialVersionUID。 SerialVersionUID 是序列化和反序列化兼容性的保证。

java.lang.ClassNotFoundException 不会因序列化过程中的错误而引发。

2) SUID 基本上是类名、接口(interface)类名、方法和字段的哈希值,类中的任何更改都会导致不同的 SUID。您可以编写自己的算法来计算该值,但不建议这样做。

可以引用这个link进一步探索serialVersionUID。

引用此tutorial用于了解 SUID 的用法。

关于Java序列化不同的Java版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41563027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com