• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java event on placing a file in a folder?  RSS feed

 
kapil Gupta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement to store an image/text file in Database whenever a new file is dropped in a particular folder in filesystem. One way could be to use a thread that will scan the folder after a particular time period. Is it possible to listen to an event that will be generated whenever a file is placed? That way I wont have to continously scan and the thread will wakeup only when a file is actually placed.
Thanks,

Kapil
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there's something new in the latest version of Java, there is no pure Java way to get notified of files arriving in folders. Therefore, your choices are to periodically scan the folder, as you suggest, or to write native code to pick up your particular platform's native events on file movements, then interface that to your Java with JNI.

I've done a file scanner previously with success. You need to take care that the file has finished copying before trying to process it. Look at the size of the file over several scans, before concluding it's stable. Otherwise, you may try to process half a file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!