java 反序列化抛出EOFException,如何多次序列化与反序列化同一个对象,将对象存储在文件中?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentstorage;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
ObjectInputStream in1 = null;
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("e:/java/storage.dat"));
ObjectInputStream in = new ObjectInputStream(new FileInputStream("e:/java/storage.dat"));
Object obj =new Object();
Storage storage = new Storage();
System.out.println(storage);
int i;
boolean flag = true;
try {
while (flag) {
System.out.println("**************************");
System.out.println("1、查询总库存量。\t");
System.out.println("2、输入入库货物数量。\t");
System.out.println("3、输入出库货物数量。\t");
System.out.println("4、退出。\t");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入你的选择:");
i = Integer.parseInt(reader.readLine());
switch (i) {
case 1:
obj=(Object)storage;
FileInputStream fis = new FileInputStream("e:/java/storage.dat");
in = new ObjectInputStream(fis);
try {
if (fis != null) {
obj = in.readObject();
System.out.println("sssssssssssssssssssssssss");
// in.close();
} else {
System.out.println("库存为空!");
}
} catch (EOFException ex) {
System.out.println("读取完毕!");
} finally {
in.close();
}
storage = (Storage) obj;
if (storage != null) {
System.out.println("总库存量=" + storage.getGross());
}
break;
case 2:
obj=(Object)storage;
System.out.println("请输入入库货物数量");
int num1 = Integer.parseInt(reader.readLine());
fis = new FileInputStream("e:/java/storage.dat");
in = new ObjectInputStream(fis);
if (fis != null) {
try {
obj = in.readObject();
} catch (EOFException ex) {
System.out.println("读取完毕!");
storage = (Storage) obj;
storage.inputGoods(num1);
out.writeObject(storage);
} finally {
in.close();
out.close();
}
}
break;
case 3:
obj=(Object)storage;
System.out.println("输入出库货物数量");
int num2 = Integer.parseInt(reader.readLine());
fis = new FileInputStream("e:/java/storage.dat");
in = new ObjectInputStream(fis);
obj = in.readObject();
in.close();
storage = (Storage) obj;
storage.outputGoods(num2);
out.writeObject(storage);
out.close();
break;
case 4:
flag = false;
break;
default:
throw new NumberException("输入的数不合法!请重新输入:");
}
}
} catch (NumberException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (in1 != null) {
in1.close();
}
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentstorage;
import java.io.Serializable;
/**
*
* @author Administrator
*/
public class Storage implements Serializable {
private int gross;//库存量
public int getGross() {
return gross;
}
public void setGross(int gross) {
this.gross = gross;
}
public Storage() {
}
public Storage(int gross) {
this.gross = gross;
}
public void inputGoods(int stock){
gross+=stock;
}
public void outputGoods(int shipment){
gross-=shipment;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentstorage;
/**
*
* @author Administrator
*/
public class NumberException extends Exception {
public NumberException() {
}
public NumberException(String message) {
super(message);
}
}
我想要的功能是:将对象序列化到storage.dat中,程序关闭后再次运行,能够反序列化文件中的对象。而现在只能在程序运行时反序列化,一旦程序关闭,下次再运行时,反序列化后,对象的gross总是为0,我怀疑是没有读取到,跪求各位大侠援助啊!!! EOFException 反序列化 java 多次存储 readObject
补充:Java , Java SE