(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.
  • #1
icecubebeast
66
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
  • #2
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.
 
  • #3
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.
 
  • #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
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:
  • #7
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.
 

1. How do I create a program that will automatically reload in Java?

To create a program that will reload in Java, you can use the Timer class and the TimerTask interface. First, create a Timer object and specify the interval at which you want the program to reload. Then, create a TimerTask object that contains the code you want to execute when the timer expires. Finally, schedule the TimerTask with the Timer, and your program will reload at the specified interval.

2. Can I specify a specific time for my program to reload?

Yes, you can specify a specific time for your program to reload by using the schedule method of the Timer class. This method allows you to specify a Date object as the time for the program to reload. You can also specify a fixed delay or a fixed rate for the reloading.

3. How can I make my program reload continuously without any user interaction?

You can make your program reload continuously without any user interaction by using the scheduleAtFixedRate method of the Timer class. This method allows you to specify a fixed rate at which the program should reload, regardless of how long the previous reload took.

4. Is it possible to cancel the reload of my program?

Yes, it is possible to cancel the reload of your program by calling the cancel method of the Timer object. This will stop the timer and prevent any future reloads from occurring. You can also use the TimerTask's cancel method to stop a specific reload task from executing.

5. Are there any alternatives to using the Timer class for program reloading in Java?

Yes, there are other alternatives to using the Timer class for program reloading in Java. Some popular options include using the ScheduledExecutorService interface, which allows for more control and flexibility in scheduling tasks, or using a third-party library like Quartz or Spring's TaskScheduler.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
919
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
726
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
Replies
5
Views
1K
Back
Top