Search code examples
javaserialization

Serializing an object containing an array and an int


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 which can take upwards of 20 minutes. 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. 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.

P.S. I have edited the code to better Explain my problem as it sems it isn't clear enough. 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();
            }

Solution

  • You are trying to deserialize from an empty file.