Problem with folder watcher
Hello, new to Java but not entirely new to programming in general. This is my first application and am running into an issue. I'm designing my program to watch a specific folder for new text files that our clients drop off. It checks the folder every half second for files it hasn't seen before and when it detects a new file its gets processed.
It works great for files under 20 KBs or so, but anything larger than that the program interferes and corrupts the file as it is being placed in the folder. Is there anyway for my program to not notice a file until after it has been completely loaded to the folder? Right now I am checking every half second for new files because the timer doesn't seem to work for anything larger than a second, I've done some research and figured out how to increase the time using scheduled events but that doesn't really solve the problem. I could have it check once an hour and there would still be that small chance that the client drops a file off at the exact time my program checks the folder.
Here is the relavent code, workedFiles is a map to keep track of what files the script has seen before. The problem is definately this section, I've taken out all processing of files and the corruption and app crash still occurs:
Code:
public void actionPerformed( ActionEvent actionEvent) {
File directory = new File(fileDirectory);
String d[] = directory.list();
for (int i=0; i<d.length; i++) {
if (d[i].contains(".txt")) {
if (workedFiles.containsKey(d[i]) == false) {
workedFiles.put(d[i], new Integer(1)); // add file to hashmap
processFile(d[i]); // still happens even if I comment this out
}
}
}
}