使用java序列化保存/加载对象数组
我有以下类,它执行一些计算来填充其静态数组。
public class Amount implements Serializable{
private static final long serialVersionUID = 8141477444408242243L;
public static Amount values1[][] = new Amount[10][30];
public static Amount values2[][] = new Amount[10][30];
public static Amount values3[][] = new Amount[10][30];
double highestValue;
double highestAmount;
double lowestAmount;
double lowestValue;
...
}
由于计算需要 20 分钟左右,我希望将数组存储在文件中并在程序启动时加载值。我正在尝试使用 java 序列化方法,并具有以下函数
public static void loadFile(Amount[][] arr, String filename){
try {
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
arr = (Amount[][])in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e);
}
}
public static void saveFile(Amount[][] arr, String filename){
try {
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(arr);
out.flush();
out.close();
}
catch (IOException e) {
System.out.println(e);
}
}
,我称之为 saveFile(values1, "valueOneSaveFile");
和 loadFile(values1, "valueOneSaveFile");
我已运行该程序一次,将所有数组保存到各个文件中。文件已创建并且看起来大小正确。当我更改程序以调用 loadFile 函数时,数组似乎未正确初始化。尝试从数组中读取值时出现空指针异常(加载后该数组似乎为空)
I have the following class, which performs some calculations to fill its static arrays.
public class Amount implements Serializable{
private static final long serialVersionUID = 8141477444408242243L;
public static Amount values1[][] = new Amount[10][30];
public static Amount values2[][] = new Amount[10][30];
public static Amount values3[][] = new Amount[10][30];
double highestValue;
double highestAmount;
double lowestAmount;
double lowestValue;
...
}
As the calculations take 20 minutes or so, I am looking to store the arrays on file and load the values when the program starts. I am attempting to use the java serialization method and have the following functions
public static void loadFile(Amount[][] arr, String filename){
try {
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
arr = (Amount[][])in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e);
}
}
public static void saveFile(Amount[][] arr, String filename){
try {
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(arr);
out.flush();
out.close();
}
catch (IOException e) {
System.out.println(e);
}
}
which I call like this saveFile(values1, "valueOneSaveFile");
and loadFile(values1, "valueOneSaveFile");
I have run the program once, saving all the arrays to various files. The files have been created and look to be around the correct size. When I change my program to call the loadFile functions, the arrays do not appear to initialize correctly. I am getting null pointer exceptions when trying to read a value from the array (which appears to be empty after the load)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在您的 LoadFile 方法中。
Java 按值传递参数。对于对象,传递“指针”的副本。
当您更新数组时:
您没有更新 Amount.values1 数组,而是本地 arr 变量指向新数组。
您应该将方法签名更改为:
并相应地使用它。
The problem is in your LoadFile method.
Java passes parameters by value. In the case of objects a copy of the "pointer" is passed.
When you update the array:
You are not updating Amount.values1 array, instead the local arr variable points to a new array.
You should change the method signature to:
And use it accordingly.
这可能是您在
Amount
类中实现的readObject
和writeObject
方法的问题。可以在此处找到一个全面的示例。您还可以考虑使用 XStream 保存/加载数据。它非常易于使用,如此处所示。
It might be an issue with the
readObject
andwriteObject
methods that you are implementing in yourAmount
class. A comprehensive example can be found here.You might also consider using XStream to save/load your data. It is very easy to use, as shown here.