Timer in a program, and Deleting a file

Posted on 2005-03-30
Medium Priority
Last Modified: 2010-03-31
I need to set my program to check for files in a directory every 2 seconds and if something is there then I will process it otherwise wait.  This program will basically run forever (or until the computer crashes).

I also need to know how to delete a file in Java.  I am very new so forgive me for asking trivial questions.
Question by:Intern
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 15

Expert Comment

ID: 13664716
Look at java.util.Timer
and new File("path_to_file").delete(); // to delete a file
LVL 16

Accepted Solution

imladris earned 2000 total points
ID: 13664751
How about something relatively simple like:

String dfile[];
File dirc=new File(dirname);
while(/* continuecondition */)
{   Thread.sleep(2000);           // delay for 2 seconds
     dfile=dirc.list();                  // get list of files in directory "dirname"
     for(int i=0; i<dfile.length; ++i)
     {   if( /* dfile[i] needs processing */)
         {   // process file

This while loop waits for 2 seconds, gets a list of the files in the directory sepcified for dirc, then checks each one to see if it needs to be processed or not.
Some files returned may not actually need to be processed such as "." and "..".
LVL 16

Expert Comment

ID: 13664783
So, for /* continuecondition */ you would need to fill in some details.
The while condition controls how long the program runs. You may want to run it for some fixed amount of time (10 hours) or until it receives some signal to stop.
For /* dfile[i] needs processing */ you would presumably fill in some condition based on the file name.

Oh, and to delete a file you can also use a File object:

File df=new File(filename);

Expert Comment

ID: 13665652

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question