I am planning to build a program which keeps on looking the directory, and if any new file comes then it'll notify me. But I don't want to use Watch Service API. Is there any other simple logic to do the same? without using any third party API or library?
Asked
Active
Viewed 42 times
0
-
1Just curious, why not use the WatchService? And why ask the question regarding same thing multiple times (just an attempt to gain repo points??)? Question that you already asked: http://stackoverflow.com/questions/34553881/how-to-check-whether-files-are-there-or-not-continuously-in-java – Aaditya Gavandalkar Jan 01 '16 at 08:28
-
Here my question is about state sequence. Suppose, I want to change the sequence from 1-2-3-4 to 1-2-4-3 then without changing in the code, how can I do that? – Avid Learner Jan 07 '16 at 18:09
1 Answers
1
The Watch Service API is the simplest and most efficient way to do this, which is why it exists.
You can however, poll the directory and look for changes. This has some down sides.
- It is hard to detect all possible changes.
- It can be really inefficient and create a lot of garbage if you do this regularly or have a lot of files or both.
- It can be error prone if you haven't implemented this before.
Is there any other simple logic to do the same?
Repeatedly call
File dir = new File(directoryName);
// note: creates a lot of garbage even if nothing changed.
File[] files = dir.listFiles();
if (files != null) {
// check for changes.

Peter Lawrey
- 525,659
- 79
- 751
- 1,130