English 中文(简体)
使用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;

      ...
}

由于计算时间为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(价值1,“价值1SaveFile”);loadFile(价值1,“价值1”,;

我曾经管理过该方案,将所有阵列都保存到各种档案中。 这些档案已经建立起来,正视准确的规模。 当我改变称为负荷功能的方案时,这些阵列似乎并没有正确开始。 我在试图从阵列中读出一种价值时,正在破获一些例外(在装载后似乎空洞)。

最佳回答

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();

你们没有更新数额。 价值1阵列,而地方的rr变点则是新的阵列。

您应将该方法签名改为:

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

因此使用。

问题回答

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.

您也可考虑使用XStream,以节省/载荷您的数据。 如here,这非常容易。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签