Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

581 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