我正在做一个涉及Java序列化和反序列化的练习。在程序执行过程中,我从IDE的控制台(Netbeans)中收到以下错误:
set 08, 2018 7:15:23 PM serializationtest.Serializator deserialization
Name: soap powder
GRAVE: null
Value: 10.44
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1381)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at serializationtest.Serializator.deserialization(Serializator.java:66)
at serializationtest.SerializationTest.main2(SerializationTest.java:22)
at serializationtest.SerializationTest.main(SerializationTest.java:9)
我搜索了类似的问题,但是发现的所有内容都涉及Internet连接,套接字和种类的使用,而我的程序要比这简单得多。其他来源(例如https://stackoverflow.com/a/2395269/10335284)告诉我,创建第二个ObjectOutputStream
实例时会发生此错误。但是我已经测试了每个程序执行仅使用一个ObjectOutputStream
(请参阅本文的结尾),即使如此,也会发生相同的错误。
以下是我遇到问题的程序的代码。
类序列化测试:
public class SerializationTest {
public static void main(String[] args) {
main1();
main1();
main2();
}
public static void main1(){
ManagerProduct mProd = new ManagerProduct();
mProd.productBuild("soap powder", 10.44);
Serializator ser = new Serializator(mProd);
ser.serialize();
}
public static void main2(){
ManagerProduct gProd = new ManagerProduct();
Serializator ser = new Serializator(gProd);
ser.deserialization();
gProd.productsPrint();
}
}
Class Serializator:
public class Serializator {
FileOutputStream fos;
ObjectOutputStream oos;
FileInputStream fis;
ObjectInputStream ois;
ManagerProduct gProd;
Product product;
public Serializator(ManagerProduct gProd) {
this.gProd = gProd;
try{
fos = new FileOutputStream("mydata.dat", true);
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void serialize(){
try {
fos = new FileOutputStream("mydata.dat", true);
oos = new ObjectOutputStream(fos);
for(Product produtoIt : gProd.products){
oos.writeObject(produtoIt);
}
oos.close();
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void deserialization(){
try {
fis = new FileInputStream("mydata.dat");
ois = new ObjectInputStream(fis);
while(true){
product = (Product) ois.readObject();
if(product != null){
gProd.productAdd(product);
}else{
break;
}
}
ois.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
if(ex instanceof EOFException){
System.out.println("End of file has been reached, unable to read object!");
return;
}
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Serializator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
一流的产品:
public class Product implements Serializable{
String name;
double value;
public Product(String name, double value) {
this.name = name;
this.value = value;
}
}
Class ManagerProduct:
public class ManagerProduct {
List<Product> products = new ArrayList();
public void productBuild(String nome, double valor){
products.add(new Product(nome, valor));
}
public void productAdd(Product produto){
products.add(produto);
}
public void productsPrint(){
for(Product product : products){
System.out.println();
System.out.println("Name: "+product.name);
System.out.println("Value: "+product.value);
}
}
}
为澄清起见,如果我删除“ mydata.dat”(以“重新开始”)并将类SerializationTest
中的main()方法替换为
public static void main(String[] args) {
main1();
main2();
}
程序将在第二次运行时抛出相同的异常(在第一次运行时,它不会抛出任何异常)。
PS:如果我的英语不好,我不是来自英语国家并且还在学习,我对此表示歉意。