0

I've created a little program with BlueJ that works as a digital registry (registry as in 'a sorted list'). The program has only two classes: one working as a template for the entries in the registry which in return is represented by an ArrayList in the second class, the one managing all entries. E.g. the user can create a new entry via the console and later search for the right entry and read the details over the console, thus this entries are instances of the first class "Entry".

Now I've exported the .jar and I do can use all features as in the console-simulation but since the instances aren't saved actually but rather for only one session this program has no useful functionality. I somehow need to save those created instanced in order to have them already in the ArrayList everytime I run the program. I'm searching for something that does like the "save world" functionality does in Greenfoot.

I've read that you can save objects with the help of the testclass in BlueJ: Saving Objects with the testclass. But the problem is that I can't actually "see" those entries since they are stored in the ArrayList and that I later want to run the program completely independent (i.e. without BlueJ but the .jar).

Does anybody know how I can save those instances and use them later on when I quit and run the program again?

Yeliz
  • 25
  • 8
  • 1
    Possible duplicate of [What's the easiest way to persist java objects?](http://stackoverflow.com/questions/1955396/whats-the-easiest-way-to-persist-java-objects) – azurefrog Dec 10 '15 at 23:47
  • There is no "save the world" feature IRL. You'll have to program the save and load feature the way you want it to work. In the end, it's some form of writing the data of the instances into a file (text file, database file, ...) and later reconstructing the objects from the data in a file. – zapl Dec 10 '15 at 23:56

2 Answers2

0

To store all of your instances, you can use choose one of the following ways:

  1. Save to file using Java Serialisation API - however, you need to implement the Serializable interface in all the classes,

  2. Save to file as xml or json using some library like GSON, Jackson, JAXB, etc.

  3. Save to database using some ORM like JPA, JDO, nosql, etc.

Mike
  • 522
  • 2
  • 14
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
0

I had to go look at what Greenfoot "Save the world" meant.

I think you're looking to use Java Serialization.

Program:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    File file = new File("address.ser");
    file.deleteOnExit();
    FileOutputStream fout = new FileOutputStream(file);

    String addressForWriting = "123 Fake Street";
    try ( ObjectOutputStream oos = new ObjectOutputStream(fout) ) {
        oos.writeObject(addressForWriting);
    }

    FileInputStream fin = new FileInputStream(file);
    try ( ObjectInputStream ois = new ObjectInputStream(fin) ) {
        String addressForReading = (String) ois.readObject();
        System.out.println(addressForReading);
    }
}

Output:

123 Fake Street

In this case, String implements the Serializable interface which allows Java to write it out as an object and then read it back in as an object. I don't know anything about your two classes. However, if you get them to implement the Serializable interface (including their instance variables), then you should be able to write all Java objects out to a file.

Mike
  • 522
  • 2
  • 14
  • Serialization is what I'm looking for but there's still a problem: there are a few [ways](https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/examples/index.html) to use Serialization. In this case you've chosen the [writeObject method](https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/examples/custom/index3.html) which doesn't allow versioning. There is also a [SF API method](https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/examples/altimpl/index3.html). Don't I need ladder since I'll most definetly edit the ArrayList & thus the objects? – Yeliz Dec 11 '15 at 20:04
  • Yep, so there are. I can't really make heads or tails of your original description. I have described a simple serialisation process, but clearly you know what serialisation is, so I guess you're looking for something more. – Mike Dec 11 '15 at 23:29