简体   繁体   English

序列化包含数组和int的对象

[英]Serializing an object containing an array and an int

So i have been trying to implement a way to save some of my Objects in a file so that I can cut down on the need for filling a variable each run time wich can take upwards of 20 minutes. 所以我一直在尝试实现一种方法来保存我的文件中的一些对象,以便我可以减少每个运行时间填充变量的需要,这可能需要花费超过20分钟。 I am currently in the midst of using an Object called a Raster which can be filled using a type File which is used to pull data into the fields. 我目前正在使用一个名为Raster的对象,它可以使用类型File填充,用于将数据拉入字段。 I am wondering how i would be able to serialize the following. 我想知道我将如何序列化以下内容。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;

public class Raster implements Serializable {

    private static final long serialVersionUID = 15L;
    private int col,row,NODATA;
    private double [] [] Ras;
    public Raster (File inData) throws IOException
    {
    //open file as f
    BufferedReader f = new BufferedReader(new FileReader(inData));
    this.col = Integer.parseInt(f.readLine().substring(5).trim());
    this.row = Integer.parseInt(f.readLine().substring(5).trim());
    f.readLine();
    f.readLine();
    f.readLine();
    this.NODATA = Integer.parseInt(f.readLine().substring(12).trim());
    //now the data will be added
    this.Ras = new double [row] [col];
    for (int r = 0 ; r <row;r ++ )
        {
        String[] vals = f.readLine().split(" ");
        for (int c = 0 ; c < col; c ++ )
            this.Ras[r][c]= Double.parseDouble(vals[c]);
        }
    f.close();
    }
    public int getRows() {
        return row;
    }
    public int getCols() {
        return col;
    }
    public double getData(int rowPos, int colPos) {
        return Ras[rowPos][colPos];
    }
}

I have looked at some other examples but they all seem to be rather specific to other types of data other than an array inside of an object and so i hope that someone might be able to explain a way i might be able to serialize this. 我已经看了一些其他的例子,但它们似乎都非常特定于除了对象内部的数组之外的其他类型的数据,所以我希望有人能够解释一种我可能能够序列化它的方式。

PS I have edited the code to better Explain my problem as it sems it isnt clear enough. PS我已编辑代码以更好地解释我的问题,因为它sems它不够清楚。 When using the serialize this class it would come up with an error like this: 当使用序列化这个类时,它会出现如下错误:

Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)

when i ran my code which looks like this 当我运行我看起来像这样的代码

if (rasPath.exists()) {
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(rasPath));
                Raster SqrRas =  (Raster) (in).readObject();
                in.close();         
            }
            else {
                Raster SqrRas = new Raster (fullPath);
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(rasPath));
                out.writeObject(SqrRas);
                out.close();
            }

您正在尝试从空文件反序列化。

in regards to my issue i found out it was actually due to my path name having an extension created inaccurately thus preventing the data from being located. 关于我的问题,我发现它实际上是由于我的路径名称有一个不准确的扩展,从而阻止数据被定位。 All is good 一切都很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM