0

I need to read all files in a directory every say 5 minutes and check if any new files have been created.

What would be the best way to do this?

I thought of using a DB to store all file names and then compare one by one but it seems like it would take too long.

I also thought of writing the file names to a text file and the looking if the file name is on the list or not.

Is there a better way?

Luciano Stupenengo
  • 183
  • 1
  • 1
  • 12

1 Answers1

1

Why don't you try with FileObservers

private final class DirectoryObserver extends FileObserver {

private DirectoryObserver(String path, int mask) {
    super(path, mask);
}

@Override
public void onEvent(int event, String pathString) {
    event &= FileObserver.ALL_EVENTS;
    switch (event) {
        case FileObserver.DELETE_SELF:
            //do stuff
            break;

        case FileObserver.CREATE:
        case FileObserver.DELETE:
            //do stuff
            break;
    }
}
}

Ref:FileObserver CREATE or DELETE received only for files

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29