Java – serialize objects containing arrays and ints

So I've been trying to implement a method to save some objects in my file so that I can reduce the need to fill variables at each run time, which may take more than 20 minutes I'm currently using an object called raster, which can be populated with type file to pull data into the field I want to know how I will 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've seen some other examples, but they seem to be very specific to other types of data other than arrays inside objects, so I hope someone can explain a way I might be able to serialize it

PS: I edited the code to better explain my problem because it SEMS is not clear enough When serializing this class, it will encounter the following error:

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 run code that 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();
            }

Solution

You are trying to deserialize from an empty file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>