0

Can anyone give me a quick tip on saving integers in Java. I'm making a custom clock using a timer & would like the time to save when I exit the program(or auto restart is initiated & calls the save method) & start where it left off.

my code:

method in MainClass:

        t = new Timer();
        tt = new JGameTimer();
        t.scheduleAtFixedRate(tt, 1000, 1000);

TmerClass:

    private static int timeInSeconds = 0, timeInMinutes = 0, timeInHours = 0, timeInDays = 0, timeInYears = 0;
    private int lastTimeInSeconds, lastTimeInMinutes, lastTimeInHours, lastTimeInDays, lastTimeInYears;
    private boolean timeHasBeenSet = false;

    public void run() {
        if (!timeHasBeenSet) {
        timeInSeconds = lastTimeInSeconds;
        timeInMinutes = lastTimeInMinutes;
        timeInHours = lastTimeInHours;
        timeInDays = lastTimeInDays;
        timeInYears = lastTimeInYears;
        timeHasBeenSet = true;
        }else{
        continueTime();
        }
    }

    public void saveTime() {
        lastTimeInSeconds = timeInSeconds;
        lastTimeInMinutes = timeInMinutes;
        lastTimeInHours = timeInHours;
        lastTimeInDays = timeInDays;
        lastTimeInYears = timeInYears;
    }

    public void continueTime() {
        timeInSeconds++;
        if (timeInSeconds == 60) {
            timeInSeconds = 0;
            timeInMinutes++;
        }
        if (timeInSeconds == 10) {
            saveTime();
            System.out.println("Time Saved (Seconds)");
        }
        if (timeInMinutes == 60) {
            timeInMinutes = 0;
            timeInHours++;
        }
        if (timeInMinutes == 2) {
            saveTime();
            System.out.println("Time Saved (Minutes)");
        }
        if (timeInHours == 24) {
            timeInHours = 0;
            timeInDays++;
        }
        if (timeInDays == 2920) {
            timeInDays = 0;
            timeInYears++;
        }
        System.out.println("Time is "+timeInYears+":"+timeInDays+":"+timeInHours+":"+timeInMinutes+":"+timeInSeconds+"");
    }
Ryan
  • 359
  • 2
  • 8
  • 15
  • 1
    It sounds like you want to make a little file with the last time stored in it that your program uses. – clwhisk Oct 16 '13 at 03:20

2 Answers2

2

I'd recommend using java.io.DataOutputStream with an underlying java.io.FileOutputStream. DataOutputStream provides methods to write primitive types:

DataOutputStream output = new DataOutputStream(
        new FileOutputStream(new File("my-file.dat")));
output.writeInt(someInt);
// ... write more values ...

You can later read these back (in the same order) using DataInputStream and FileInputStream.

Alternatively, you could write a class that holds the data to save and use Java's serialization mechanism to save/load it.

Torious
  • 3,364
  • 17
  • 24
  • Using [`DataInputStream.readInt()`](http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readInt()). – Torious Oct 16 '13 at 03:29
0

Well a quick tip would be to store the whole time object into a file. Implement interface Serializable, then search the web on how to store/read serializable objects with a ObjectOutputStream. For adding code on shutdown have a look at Running code on program exit in Java.

Community
  • 1
  • 1
wtfmoments
  • 16
  • 1