Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Monitoring Folders

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic