Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jan 5, 2015 #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. jcsd
  3. Jan 5, 2015 #2


    Staff: Mentor

    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.
  4. Jan 5, 2015 #3
    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.
  5. Jan 5, 2015 #4


    Staff: Mentor

  6. Jan 6, 2015 #5


    User Avatar
    Gold Member

    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:


    Those are the values which get persisted to the disk (form size and placement).
    Last edited: Jan 6, 2015
  7. Jan 11, 2015 #6


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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: Jan 11, 2015
  8. Jan 11, 2015 #7


    User Avatar
    Gold Member

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook