File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Monitoring Folders

 
hatim osman
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
Is there way I can monitor a particular folder, new files added, renamed or deleted? I want so some sort of a listener, if possible of course.

thank you
 
Adam Teg
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently doing a simillar thing. I implemented a Thread(s) that wakes up every 10 seconds to check if a file came in or not. Looks like in your case you will need to know what files were there to begin with etc.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know of no way to get the operating system to do a call-back to your Java program when the file system is modified so it looks like you will end up using the methods in java.io.File.

A File object can designate a directory or a disk file. It has the method lastModified() - I wonder what happens when you call lastModified on a File object that represents a directory??

Bill
 
Vinod Awar
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write an interface Monitor with methods like addMonitor and removeMonitor
and you can monitor the folder for any type of files by listing all files in the folder and checking their extension. If found you can invoke a filefound() method or a listener registered with this. Just a vague idea
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hatim osman:
Hi there
Is there way I can monitor a particular folder, new files added, renamed or deleted? I want so some sort of a listener, if possible of course.

thank you


Other actions can be deduced but i am really not sure about how a rename action can be deduced i.e. how do you differentiate between a file deleted and a new created and one is renamed to other? I dont think there is a way to find out whether the file/directory has been renamed unless OS gives you a callback for this(which as william pointed out is not available.)
Since, you can not have a listener for directory modification events, so you have to write a deamon that wakes up after a configured interval of time and checks the state of the dir with the last state.
There can be an optimization(as william pointed) that you can check the last modified time of the directory with the last time it was scanned. But, this depends on the OS whether it updates the time modified of the directory for actions taken on any of the files and sub-directories directly by the OS or through other application. (I validated it with windows vista, the directory time does not change if i modify any of its file using my IDE.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic