views:

58

answers:

1

i have one property file which contains some records so i need to delete the records after some time interval let's take 2 minute or 3 minutes, so how can i do that one?

A: 

You should create a timer task to perform your action at a future date. Take a look at the Timer API.

final Properties props = new Properties();
props.setProperty("key", "value");
props.store(outputStream, "Adding key=value");
TimerTask deletePropertyTask = new TimerTask() {
    public void run() {
        props.remove("key");
        props.store(outputStream, "Removing key");
    }
};
new Timer().schedule(deletePropertyTask, 3000L); // fire in 3 seconds

I tested a roughly equivalent version of this in the Scala REPL. Here it is:

import java.util._
import java.io._
val props = new Properties
props.setProperty("key", "value")
props.store(System.out, "Message")
val task = new TimerTask() {
  def run = {
    props.remove("key")
    props.storeToXML(System.out, "no comment")
  }
}
new Timer().schedule(task, 3000l)
// the properties file is rewritten (to System.out) some 3 seconds later.
Synesso
@hey thanks Synesso i tested this code thanks for ur help!!!!!!!
vinod