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

发布于 2024-12-11 12:09:44 字数 1512 浏览 0 评论 0原文

我有以下类,它执行一些计算来填充其静态数组。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-12-18 12:09:44

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

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

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

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

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

并相应地使用它。

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.

戏剧牡丹亭 2024-12-18 12:09:44

这可能是您在 Amount 类中实现的 readObjectwriteObject 方法的问题。可以在此处找到一个全面的示例。

您还可以考虑使用 XStream 保存/加载数据。它非常易于使用,如此处所示。

It might be an issue with the readObject and writeObject methods that you are implementing in your Amount 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文