1

I wrote program for reading and displaying GIS shape files by GeoTools, and I want to save the state of my program.

Main class.java

        Project myproject=new Project();
        myproject.mapLayers=this.frame.getMapContext().getLayers();

Project.java

import org.geotools.map.MapLayer;    
public class Project implements Serializable{
    public String name;
    public  MapLayer[] mapLayers;

    public void save(String projectname){
        ReadWriteObject.writeObject(projectname+".gpr",this);
    }

    public Project load(String projectname){
        return (Project)ReadWriteObject.readObject(projectname);
    }
}

ReadWriteObject

public class ReadWriteObject {

    public static boolean writeObject(String filename, Object obj){
        try {
            FileOutputStream f = new FileOutputStream(new File(filename));
            ObjectOutputStream o = new ObjectOutputStream(f);

            // Write objects to file
            o.writeObject(obj);
            o.close();
            f.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            return false;
        } catch (IOException e) {
            System.out.println("Error initializing stream");
            return false;

        }

        return true;
    }
}

Error occurred DefaultMapLayer is not Serializable. How do I save my programs state in java?

Ian Turton
  • 10,018
  • 1
  • 28
  • 47

1 Answers1

0

I would simply write out in some format (XML, JSON) the layers, styles that went into your map and then read that back in again to recreate the map. You don't actually need to store the entire state of the program.

Alternatively, you can propose a change to the source code to add Serialize to class. Ideally you should find all of the necessary classes that will need serializing that currently don't implement Serialize and submit a single pull request.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47