Serializing an object containing an array and int

So, I tried to implement a way to save some of my objects in a file so that I could reduce the need to fill out a variable for each runtime, which can take up to 20 minutes. I am currently using an object called Raster, which can be populated with a file of the type that is used to pull data into fields. I am wondering how I could 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 looked at some other examples, but they all seem pretty specific to other types of data other than an array inside an object, and so I hope someone can explain the way I could serialize this.

P.S. , , sems, . :

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)

,

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();
            }
+4
2

.

+1

, , - , , , .

-1

Source: https://habr.com/ru/post/1687378/


All Articles