S
smartnhandsome
Hi All,
I want to use TimerTask to watch for a file in a particular folder.
and proceed to another line of code only after i get the file in a
particular folder. The TimerTask does it well but a seperate thread i
guess I want to
new FileWatcher("c://temp3//",file,1000);
do this line1;
do this line2;
I want to do this line1 and do this line2 only after i get the file
i.e. I want to return from watchFile method only after get the file.
This code now starts to watch for a file as a seperate thread so the
do this line1 do this line2 also Execute while the filewatcher class
is still looking for the class.
Any Suggestions. Any Help Is appreciated.
public class FileWatcher {
Timer timer;
private String fileToWatch;
private String directorytoWatch;
private int timeDelay;
public FileWatcher(String directorytoWatch, String file, int
timeInterval) {
this.fileToWatch = file;
this.directorytoWatch = directorytoWatch;
this.timeDelay = timeInterval;
timer = new Timer();
TimerTask timerTask =new FileWatcherTask();
timer.schedule(timerTask, 0, // initial delay
1 * timeDelay); // subsequent rate
}
class FileWatcherTask extends TimerTask {
public void run() {
File theDirectory = new File(directorytoWatch);
File[] children = theDirectory.listFiles();
// Store all the current files and their timestamps
for (int i = 0; i < children.length; i++) {
File file = children;
if (file.getName().equals(fileToWatch)) {
System.out.println("File Found");
System.exit(0);
} else {
System.out.println("File " + fileToWatch+ " not found yet");
}
}
theDirectory = null;
children = null;
System.gc();
}
}
}
I want to use TimerTask to watch for a file in a particular folder.
and proceed to another line of code only after i get the file in a
particular folder. The TimerTask does it well but a seperate thread i
guess I want to
new FileWatcher("c://temp3//",file,1000);
do this line1;
do this line2;
I want to do this line1 and do this line2 only after i get the file
i.e. I want to return from watchFile method only after get the file.
This code now starts to watch for a file as a seperate thread so the
do this line1 do this line2 also Execute while the filewatcher class
is still looking for the class.
Any Suggestions. Any Help Is appreciated.
public class FileWatcher {
Timer timer;
private String fileToWatch;
private String directorytoWatch;
private int timeDelay;
public FileWatcher(String directorytoWatch, String file, int
timeInterval) {
this.fileToWatch = file;
this.directorytoWatch = directorytoWatch;
this.timeDelay = timeInterval;
timer = new Timer();
TimerTask timerTask =new FileWatcherTask();
timer.schedule(timerTask, 0, // initial delay
1 * timeDelay); // subsequent rate
}
class FileWatcherTask extends TimerTask {
public void run() {
File theDirectory = new File(directorytoWatch);
File[] children = theDirectory.listFiles();
// Store all the current files and their timestamps
for (int i = 0; i < children.length; i++) {
File file = children;
if (file.getName().equals(fileToWatch)) {
System.out.println("File Found");
System.exit(0);
} else {
System.out.println("File " + fileToWatch+ " not found yet");
}
}
theDirectory = null;
children = null;
System.gc();
}
}
}