3

I have a WatchService implementation where the service watches for the directory using key = watchService.poll() or take() methods.. But issue I am facing is, what if I have some files already present into the watch directory before starting poll() or take() method. WatchService unable to track those. How do I get those files under a directory? Is there anything WatchService provides to achieve this? Or any other solution to this challenge?

Thanks in advance!

Saurabh Deshpande
  • 1,153
  • 4
  • 18
  • 35
  • What do you mean "WatchService unable to track those" ? You should be notified about changes. – Oleg Nov 29 '17 at 11:40
  • I don't know much about `WatchService` but it's easy enough to read all files in a directory; https://stackoverflow.com/questions/1844688/how-to-read-all-files-in-a-folder-from-java – achAmháin Nov 29 '17 at 11:41
  • @Oleg - it notifies about file event once start polling it.. I am saying before starting a poll, what if we have some files already dumped into a dir.. is there any solution present in WatchService – Saurabh Deshpande Nov 29 '17 at 11:45
  • @notyou - yeah, that is always a open option.. i wanted to check instead of having a different block of code for it, do we have something that WatchService provides – Saurabh Deshpande Nov 29 '17 at 11:46
  • I doubt it - I presume the main purpose is to *watch* for files, and log or otherwise the changes. I would guess here you'll need a separate method, maybe just call it once before the `WatchService` starts; I imagine it's not a big overhead. – achAmháin Nov 29 '17 at 11:49

1 Answers1

1

You can use listFiles method before starting your service and you get that files names right ?

Saranya Subramanian
  • 417
  • 1
  • 5
  • 19
  • yeah, that is always a open option.. i wanted to check instead of having a different block of code for it, do we have something that WatchService provides – Saurabh Deshpande Nov 29 '17 at 12:11
  • If you list and process already existing files before starting the `WatchService`, you may miss the files coming in-between. If you start the `WatchService` first (before processing already existing files), how do you know that a file won't be processed twice (by the `WatchService` AND by your process for already existing files)? – Xavier Dury May 26 '23 at 06:52