(Java) How to make a program that will reload

  • Java
  • Thread starter icecubebeast
  • Start date
  • Tags
    Java Program
In summary, you would make a program in Java that will reload without erasing the variable data by storing the data on disk. You would then use the program's startup code to set the data values.f
  • #1
(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.
 
  • #2
(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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
  • #6
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:
  • #7
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.
 

Suggested for: (Java) How to make a program that will reload

Replies
2
Views
875
Replies
5
Views
1K
Replies
1
Views
391
Replies
9
Views
1K
Replies
13
Views
598
Replies
2
Views
808
Back
Top