?
Solved

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

Posted on 2003-03-28
5
Medium Priority
?
239 Views
Last Modified: 2010-05-18
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
Comment
Question by:JodiR
[X]
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
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8226434
Use Timer and TimerTask.
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8226438
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8226481
>>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
 
LVL 3

Expert Comment

by:wide_awake
ID: 8227157
>>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
 

Accepted Solution

by:
AIndy earned 200 total points
ID: 8230182
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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