English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2 answers

The previous poster had half the answer, although I would recommend creating a class implementation of the Runnable interface, and then passing that to a new Thread instance.

Use a File object, representing the desired directory. Use the File method list() to get an array of File objects, and then iterate through that array. I highly recommend checking each to see if it represents a file or a directory (I assume you aren't interested in new directories?). Then you can call lastModified() to get the time that it changed. If you only care about files that are NEW since your previous check, I recommend keeping the previous array, and then just comparing the two to determine which are "new".

class CheckFilesRunnable implements Runnable{
private volatile boolean fStop = false;
private File fDirectory;

public CheckFilesRunnable(File dir){
fDirectory = dir;
}

public void run(){
while (!fStop){
File[] files = fDirectory.list();
//iterate, check for file vice directory
//then check time of last mod
for (int i=0;i if (files[i].isFile()){
long modified = files[i].lastModified();
//do your time comparison here...
}
}
}
}

public void stop(){
fStop = true;
}
}

//then in your code where you want to spawn the new thread...
Thread fileChecker = new Thread(new CheckFilesRunnable(dir));
fileChecker.start();

For the actual time comparison, you could keep a long to track the previous time check and compare against, or you can do something else like compare the time last modified to the time 2 hours ago, etc.

2006-09-14 18:36:45 · answer #1 · answered by vincentgl 5 · 0 0

The thread part is pretty straightforward. You can subclass from the Thread class and write your own run method. In run(), call sleep() with a two hour time period.

To list the latest files in a folder, I'd be tempted to go out to the command line. On unix, "ls -tc /path" might be what you want. To run it, lookup the exec() method of the Runtime class.

2006-09-13 04:43:28 · answer #2 · answered by arbeit 4 · 0 0

fedest.com, questions and answers