java 使用java序列化保存/加载对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7860767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:44:36  来源:igfitidea点击:

saving/loading array of objects using java serialization

javaserializationfile-io

提问by Wozza

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

由于计算需要 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); 
      }
}

which I call like this saveFile(values1, "valueOneSaveFile");and loadFile(values1, "valueOneSaveFile");

我这样调用saveFile(values1, "valueOneSaveFile");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)

我已经运行了一次程序,将所有数组保存到各种文件中。文件已创建并且看起来大小正确。当我更改程序以调用 loadFile 函数时,数组似乎没有正确初始化。尝试从数组中读取值时出现空指针异常(加载后似乎为空)

回答by Erel

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:

问题出在您的 LoadFile 方法中。Java 按值传递参数。在对象的情况下,传递“指针”的副本。更新数组时:

arr = (Amount[][])in.readObject();

You are not updating Amount.values1 array, instead the local arr variable points to a new array.

您没有更新 Amount.values1 数组,而是本地 arr 变量指向一个新数组。

You should change the method signature to:

您应该将方法签名更改为:

public static Amount[][] loadFile(String filename)

And use it accordingly.

并相应地使用它。

回答by npinti

It might be an issue with the readObjectand writeObjectmethods that you are implementing in your Amountclass. A comprehensive example can be found here.

您在类中实现的readObjectwriteObject方法可能存在问题Amount。可以在此处找到一个综合示例。

You might also consider using XStreamto save/load your data. It is very easy to use, as shown here.

您也可以考虑使用XStream来保存/加载您的数据。这是很容易使用,如图所示这里