使用java序列化保存/加载对象数组 [英] saving/loading array of objects using java serialization
问题描述
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;
$ b $ ...
}
分钟左右,我正在寻找存储阵列的文件,并加载程序启动时的值。我尝试使用java序列化方法,并具有以下功能:
pre $ public static void loadFile(Amount [] [] arr,字符串文件名){
尝试{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
arr =(Amount [] [])in.readObject();
in.close();
}
catch(Exception e){
System.out.println(e);
}
}
公共静态无效saveFile的(金额[] [] ARR,字符串文件名){
尝试{
FileOutputStream中FOS =新FileOutputStream中(文件名);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(arr);
out.flush();
out.close();
}
catch(IOException e){
System.out.println(e);
$ / code $ / pre
我这样称呼 saveFile的(值1, valueOneSaveFile);
和的loadFile(值1, valueOneSaveFile);
我运行了一次这个程序,把所有的数组保存到不同的文件中。这些文件已经被创建并且看起来是正确的大小。当我改变我的程序来调用loadFile函数,数组似乎没有正确初始化。我试图从数组中读取一个值(这在加载后看起来是空的)时,我得到了空指针异常。解决方案
问题出在你的LoadFile方法中。
Java按值传递参数。在对象的情况下,指针的副本被传递。
当你更新数组时:
arr =(Amount [] [])in.readObject();
您不更新Amount.values1数组,而是本地arr变量指向一个新数组。 / b>
您应该将方法签名更改为:
public static Amount [并且使用它。
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)
解决方案 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:
arr = (Amount[][])in.readObject();
You are not updating Amount.values1 array, instead the local arr variable points to a new array.
You should change the method signature to:
public static Amount[][] loadFile(String filename)
And use it accordingly.
这篇关于使用java序列化保存/加载对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!