• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

How do I watch a directory for a file to appear in java?

I want to have a Java application running that will continuously watch several directories for files to appear, once they appear the applicatiion will do some processing on them. I figure I can code this but thought I'd ask so I don't run into any problems with the computer freezing up, high overhead etc. Any proven code out there?
0
JodiR
Asked:
JodiR
  • 2
  • 2
1 Solution
 
CEHJCommented:
Use Timer and TimerTask.
0
 
wide_awakeCommented:
I'd suggest having the Java program check for new files at specific intervals (depending on how frequently these files will be generated), then process any new files, then go back to sleep until the next interval.

You'll also want to maintain a list (Vector?) of filenames you've already processed, so you don't keep processing the same ones over & over.
0
 
CEHJCommented:
>>then go back to sleep until the next interval.

no need to implement that yourself, i.e. reinvent the wheel. That's what TimerTask is for.
0
 
wide_awakeCommented:
>>no need to reinvent the wheel.

agreed 100%.  That's one of the best things about Java.  the above comment was just a rough description of what should happen.  The implementation was left as an exercise to the reader ;)
0
 
AIndyCommented:
Hi JodiR,

try this code :

import java.io.*;
import java.util.*;

public class FileWatcher
extends Thread
{
     
     Vector processedFiles = new Vector();
     File[] directories;
     int interval;
     
     
     /* Constructor*/
     public FileWatcher(File[] directories, int intervalInSeconds)
     {
          this.directories = directories;
          this.interval = intervalInSeconds;
          this.run();
     }
     
     
     /* Processes a file ... */
     private void processFile(File file)
     {
          System.out.println (file.getName());
         
          processedFiles.add(file);
     }
     
     
     /* defines if File should be processed,
      * in this example it processes all files
      * which haven't been yet processed */
     private boolean isTargetFile(File file)
     {
          return !processedFiles.contains(file);
     }
     
     
     /* Checks all defined directories for
      * target files */
     public void checkDirectoriesForFiles()
     {
          System.out.println ("checking for files");
         
          for (int d = 0; d<this.directories.length; d++)
          {
               File[] files = this.directories[d].listFiles();
               for (int i = 0; i<files.length; i++)
              {
                    if (this.isTargetFile(files[i]))
                    {
                         this.processFile(files[i]);
                         
                    };
              } // for i
                       
         } // for d
         
         
          try
         {
               this.sleep(interval * 1000);                  
         } // try
         catch (InterruptedException e)
         {
              e.printStackTrace();
         } // catch
     }
     
     
     public void run()
     {
          while(true)
          {
               checkDirectoriesForFiles();
          }
     }
     
     
     public static void main(String[] args)
     {
          File[] directories = { new File("myDirectory") };
          new FileWatcher(directories, 5);
     }


}
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now