0

I want to develop a tool which can delete all files of a folder 2 times a day - at 8:30 in morning and night. It should only delete all files permanently but not the folder.

My deletion code is working fine but I am having problem with scheduling. I have no idea how to write a scheduler code. Can anyone help me with the right code to schedule it?

public class Delete 
{ 
    public static void main(String[] args) 
    { 
        try
        { 
            Files.deleteIfExists(Paths.get("C:\\Users\\Dekstop\\Dummy")); 
//I want to delete all Files not the Folder
        } 
        catch(NoSuchFileException e) 
        { 
            System.out.println("No such file/directory exists"); 
        } 
        catch(DirectoryNotEmptyException e) 
        { 
            System.out.println("Directory is not empty."); 
        } 
        catch(IOException e) 
        { 
            System.out.println("Invalid permissions."); 
        } 

        System.out.println("Deletion successful."); 
    } 
} 
TylerH
  • 20,799
  • 66
  • 75
  • 101
Abhi
  • 1
  • 4
  • 1
    Use [ScheduledExecutorService](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ScheduledExecutorService.html). – Kartik Jul 17 '19 at 04:09
  • 2
    why not use scheduler available at OS level, rather than writing your own? OR may be use use frameworks like Quartz. – CuriousMind Jul 17 '19 at 04:22
  • I want to make it for my project so can't use OS schedulers....I am not familiar with Quartz so can you tell me more about it?? – Abhi Jul 17 '19 at 04:37
  • What if your app actually throws an error, and completely stops, along with the scheduler? That's where the OS scheduler would help - it will always trigger while the system is on – OneCricketeer Jul 17 '19 at 04:53
  • @cricket_007 can you tell me how to use OS scheduler?? I have never used it before...Thanks :) – Abhi Jul 17 '19 at 04:57
  • The answers given below are poor. In particular you should not use the poorly designed and long outdated `Calendar` class. Find the good answers in the linked original question. And next time please remember to search before posting a question. You typically find the good answers faster that way. – Ole V.V. Jul 17 '19 at 05:32

2 Answers2

1

You could use Timer.schedule(TimerTask task, Date firstTime, long period) method

setting first time to in the morning (any time) and the setting the period to 12-hours will do the job.

Amit Sharma
  • 159
  • 2
  • 8
0

go further with this Original post

public class ExecuteTimer {
  public static void main(String[] args){
       MyTimer te1=new MyTimer("My_Task1");
       MyTimer te2=new MyTimer("My_Task2");
      Timer t=new Timer();
      t.scheduleAtFixedRate(te1, 0,5*1000);
      t.scheduleAtFixedRate(te2, 0,1000);
   }
}

MyTimer.class

public class MyTimer extends TimerTask{
private String timername ;
public MyTimer(String n){
  this.timername=n;
}
@Override
public void run() {
    System.out.println(Thread.currentThread().getName()+" "+name+" the task has executed successfully "+ new Date());
    if("Task1".equalsIgnoreCase(name)){
      try {
      Thread.sleep(10000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
}
}
Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39