0

I am writing code that will produce a .dat file in Java using RandomAccessFile.

Each file is created an hour long, so after every minute, new data is added, after the hour a new file is created.

For example - each file name is of the date/time format DD-MM-YY-HH. So now it would be 05-11-13-14 and the next one would be 05-11-13-15 and so on.

In the file I am collecting 5 pieces of data and the first piece is of a long value, which is the current timestamp of that time.

What I need is to get the timestamp to print results every minute.

Here is what I have done so far;

public static void main (String [] args) throws FileNotFoundException
{ 
    try
    {
        DateFormat df = new SimpleDateFormat("dd-MM-yy-HH");
        Date date = new Date();
        System.out.println(df.format(date));

        File fileName = new File(df.format(date) + ".dat");
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

        for(int i = 0; i < 5; i++)
        {   
            //1383580800000 4/11/2013 4pm
            raf.writeLong(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis());
            raf.writeInt(10);
            raf.writeInt(2);
            raf.write((byte)1);
            raf.writeInt(3);        
        }
        raf.close();
    }
    catch(IOException iOE)
    {
        System.err.println(iOE);
    }
}
Dan
  • 2,020
  • 5
  • 32
  • 55
  • Unrelated, but I would consider a file name like `YY-MM-DD-HH` – Henry Nov 05 '13 at 14:21
  • It looks like you have the file IO working? Is your question _specifically_ about making it happen every minute? – Cruncher Nov 05 '13 at 14:24
  • Henry no I cant change the format, it has to be set that way. @Cruncher yes I am looking to get the code to produce data every minute – Dan Nov 05 '13 at 14:26
  • Why are you using a `RandomAccessFile`? You are simply creating a new file and generating the contents OR overwriting the contents if it exists. This can be done with a standard a standard `FileOutputStream` or `FileWriter`. – MadConan Nov 05 '13 at 14:26
  • @MadConan, I was told to use RandomAccessFile and have to create the files using it – Dan Nov 05 '13 at 14:27

2 Answers2

2

For simply printing the time stamp, all you have to do is date.getTime();. If you just want the epoch value there's absolutely no reason to touch any of the calendar or timezone classes.

If you want to schedule something to run every minute, I would have a look at the ScheduledThreadPoolExecutor class, or simply a Timer.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
  • I basically want the file to add data at - 05-11-13-14-00 and for each minute up to 05-11-13-14-59 and then create a new file for the next hour. It has to be done in real time – Dan Nov 05 '13 at 14:32
  • But you also want to write the actual time stamp to the file right? Either way, you can achieve this with the information in my answer. – Sebastiaan van den Broek Nov 05 '13 at 14:34
0

You can use Timer and TimerTask for this.

This will execute the run method every 60 seconds.

Start it with: new ScheduleEveryMinute().start()

import java.util.Timer;
import java.util.TimerTask;

public class ScheduleEveryMinute
{
    Timer timer;

    public ScheduleEveryMinute()
    {
        timer = new Timer();
    }

    public void start()
    {
        timer.schedule(new ScheduleTask(), 60000, 60000);
    }

    class ScheduleTask extends TimerTask
    {
        public void run()
        {
             //code to schedule
        }
    }
}
Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • While this answer is not incorrect, a ScheduledThreadPoolExecutor is favorite to Timer for reasons outlined at http://stackoverflow.com/a/409993/46375 – whaley Nov 05 '13 at 14:38