我有一个实现 Serializable 的类,但是每当我尝试将其写入磁盘时,我都无法找到为什么它会抛出此异常。我正在尝试编写 ValueConatiner.class
public class ValueContainer implements Serializable {
private static final long serialVersionUID = 2846820178517499793L;
public String name;
public MonetaryField buy;
public MonetaryField sell;
public MonetaryField revenue;
public MonetaryField cost;
public MonetaryField listing_fee;
public MonetaryField sale_fee;
public MonetaryField profit;
public int quantity;
public int investment;
public int period;
public ValueContainer(String n, MonetaryField b, MonetaryField s,
MonetaryField r, MonetaryField c, MonetaryField lf,
MonetaryField sf, MonetaryField p, int q,
int i, int per) {
name = n;
buy = b;
sell = s;
revenue = r;
cost = c;
listing_fee = lf;
sale_fee = sf;
profit = p;
quantity = q;
investment = i;
try {
period = per;
} catch(NumberFormatException e) {
System.out.println("No data. Enter a number.");
}
}
}
我试图在我持有 JMenuBar 的单独类中使用此方法将其写入磁盘。
private void saveFile() {
if(!currentFile.exists()) {
loadFile();
}
if(currentFile.exists()) {
try {
ValueContainer values = getValues();
FileOutputStream f_out = new FileOutputStream(currentFile);
ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
try {
obj_out.writeObject(values);
} finally {
obj_out.close();
f_out.close();
}
System.out.println("Saved!");
} catch(IOException e) {
System.out.println(e);
}
}
}