Hello, I am writing a thread to copy the files from a specific directory to the server. Since the files are sent to this directory by another group and I want to do the coping whenever the files are in the directory. Could you tell me how I can monitor the files in the directory and fire up the copy thread when the files are there? Thanks in advance! David
Who is creating the files in the folder in the first place? If it is a server-controlled code (some servlet), then it can issue a notify() after the files are created. The copying thread would be in a wait state, but when notified, it would copy the files to whatever their final destination is. Alternatively, you may want to do some other means of notification, such as JMS. If your server is not in control of when the files are created, then your option is to run a low priority background thread that will check for the existance of the new files and copy them to the destination. Eugene.
I love this thread! I am doing something similar so i need to know what is JMS that was suggested to do the checking for the files. another program writes the files to the directory and makes a note of this to a textfile. the java program then reads this textfile to do its processes to the file recently added to the directory. i was thinking about using a timer to check the textfile from time to time but a listener for the generated file would be much more accurate. so again what is JMS?
hi - check out this JMS link. it is used for async communication through messages. the part of your code creating the file would notify the copy thread trough a JMS message. - there is another way to achieve this: your copy thread could just remember the time it run the last time and then use the lastModifed timestampt fom the files. this way you would need to scan the full directory(structure) every time which might result in bad performance if there are lots of files/directories. k
Joined: May 25, 2001
hi again i just checked the lastModifed() on a win machine. if you create a file, the parent directorys lastModifed() is updated too. other than that it seems to work. k
If you're on Windows and have some good C++ skills in your group ... a C program can subscribe to messages from Windows about changes to directories. That's how Windows Explorer and even the file/open dialog can instantly show new files. The C program would send an event back to Java - perhaps via JNI? Donno if Unix has a similar API.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
subject: Fire up a read thread when the new file added to the directory