1

So, I made another question "Save object data to a file" and I realized that I wasn't specific enough.

The program that I'm writing would do very well for itself to keep historical data of files, so my goal is something like this:

if timePolled > midnight
   if fileExists(fileName)
       appendData()
   else
       createFile(data_dd_mm_yy) // in xml
endif

Look at me being all VB above... ick

Anyways, the object itself looks something like this:

public class IHandler{
    public double currentLoad;
    public String currentPrice;
    public String configArgs[];
}

I'm not really sure how I would go about doing this.

Community
  • 1
  • 1
A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • Why don't you use some logging framework? – adarshr May 18 '12 at 15:45
  • I'm open to suggestion, but I don't think that anything 'heavy' would really be necessary. I just need to serialize a few variables (maximum of about 6 or so) to a file every 5 minutes or so. – A_Elric May 18 '12 at 15:48
  • Your algorithm sounds fine to me, although you need to make some adjustments to account for that fact that every timestamp is after "midnight" depending on what day you are referring to. What problem are you having in implementing it? Hard to tell what the actual question here is. – matt b May 18 '12 at 15:53

2 Answers2

3

If I understand correctly, you want to know how to schedule a repeating task. This is how to do that. First you need a runnable class with the logic you want to execute in the run() function (i.e. the stuff inside your top level if statement in the VB code). Let's say there is an instance of this class named "task". Then you need to do the following:

ScheduledExecutorService exService = Executors.newScheduledThreadPool(CORE_POOL_SIZE);
exService.scheduleAtFixedRate(task, numberOfSecondsTilMidnight, 
                                        twentyFourHoursInSeconds, TimeUnit.SECONDS);

where numberOfSecondsTilMidnight is calculated based on the current system time and twentyFourHoursInSeconds is just 24*60*60 (this is how often to run it.

Hope this helps.

Sanjeev
  • 1,517
  • 1
  • 18
  • 30
2

For the serialization, Xstream (http://x-stream.github.io/) would do the trick. Then the rest you could handle with simple Java file IO.

facundofarias
  • 2,973
  • 28
  • 27
Choker
  • 875
  • 6
  • 9