Java (Java) How to make a program that will reload

  • Thread starter Thread starter icecubebeast
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary
To create a Java program that reloads without losing variable data, serialization and deserialization of objects is essential. This process involves saving data to a file on disk and retrieving it when the program restarts. A common approach is to use a Singleton object to manage persistent data attributes, which are updated at startup by reading from a serialized file. It's important to choose the correct file storage location, preferably outside of restricted directories like Program Files, to avoid data loss. For practical implementation, a working code example is available in the StoreIt.jar file, which includes the necessary classes for serialization.
icecubebeast
Messages
66
Reaction score
3
(Java) How to make a program that will reload without erasing the variable data?

How do you make a program in Java that will reload and won't erase the variable values (objects, strings, double, int, arrays and etc.)?

And also, what program ideas would you suggest me (beginner programmer) so that I can make programs that are somewhat useful? I have a hard time thinking what programs to make and it gets very boring.
 
Technology news on Phys.org
icecubebeast said:
(Java) How to make a program that will reload without erasing the variable data?

How do you make a program in Java that will reload and won't erase the variable values (objects, strings, double, int, arrays and etc.)?
If you want to make the program data persistent (available between runs of the program) you will need to store it (such as on a disk). This data storage is generally called serializing the data. Reading it back from disk is called deserialization.
 
Mark44 said:
If you want to make the program data persistent (available between runs of the program) you will need to store it (such as on a disk). This data storage is generally called serializing the data. Reading it back from disk is called deserialization.
Can you give me an example of java code that does this? What files will the data going to be stored on? I'm using eclipse standard IDE so I don't know where the .java files are located on my computer.
 
I don't know the Java API well enough to give you an example, but here's a link to a tutorial: http://www.journaldev.com/2452/java-serialization-example-tutorial-serializable-serialversionuid. I did a search on "java serialization" and found lots of links.

As far as what files the data will be written to, that's your call - you specify the stream that your program will write to. Opening that stream will create a file whose name you give it.
 
I do this by making all the persistent data be attributes of a Singleton object, which gets instantiated at program start. The attributes are updated when the program starts by reading from the serialized file stored somewhere on the disk.

There are many possible complicating factors, even if you have working code that serializes and deserializes the Singleton object correctly. For example, what do you do if the file doesn't exist yet? I have defaults for everything, and in that case, the file gets created with default values. Anytime the program shuts down, it needs to serialize the object back to disk. The storage can either be a binary file (unreadable by humans) or text, typically XML.

I am offering you some working code (see below). Persisting data is basically a pain in the butt, and non-trivial the first time to get it right, but once you have it, you always have it.

The biggest problem is where the file will be stored. I always used to store the file in the program folder. But now on Windows, Microsoft tends to make the entire Program Files subfolder non-writable by applications. Currently, Microsoft wants you to store program persistent values on a per user basis in special areas on the disk. My code does not do that, and for a reason, which is, if your computer crashes, you'll lose those values--and they cannot easily be recovered. So I insist on keeping my persistence values right in the folder with the executable file. Which now means, the program folder must be somewhere else on Windows other than under Program Files (or Program Files (x64)).

The historical misery of Microsoft's many attempts to control and arbitrate how programmers persist program data, especially per user program data, to disk would fill up an entire book.

Anyway, if you want my working code, grab the StoreIt.jar file from this web page: http://www.harbormist.com/pat/utilities.html. The source code is inside the .jar file, and you'll have to look at the startup code and the Singleton object and figure out how to adapt them. Also look at the events that file when the program shuts down. The startup code is in StoreIt.java (the run() method), and the Singleton object is called Model.java. Each static variable in Model.java is saved in a binary file. Message me offline if you have more questions.

The Model.java class serializes and deserializes itself. You'll see that whenthe main form closes, I set some values in the Model object, such as:

Model.setForm1Maximized(false);
Model.setForm1Top(this.getY());
Model.setForm1Left(this.getX());
Model.setForm1Width(this.getWidth());
Model.setForm1Height(this.getHeight());​

Those are the values which get persisted to the disk (form size and placement).
 
Last edited:
  • Like
Likes Silicon Waffle
harborsparrow said:
Anyway, if you want my working code, grab the StoreIt.jar file from this web page: http://www.harbormist.com/pat/utilities.html. The source code is inside the .jar file
I didn't see the source code (just class files) and I'm unable to decompile it for some reason.

EDIT: Finally got it decompiled.
 
Last edited:
Borg said:
I didn't see the source code (just class files) and I'm unable to decompile it for some reason.

EDIT: Finally got it decompiled.

The source code should be contained within the .jar file, but I didn't check it lately. Email me at ppalmer A T harbormist.com and I will email it to you.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
3
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
65
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K