简体   繁体   English

在Android中检查目录以查找新文件

[英]Check Directory for new files in android

I need to read all files in a directory every say 5 minutes and check if any new files have been created. 我需要每5分钟读取一次目录中的所有文件,并检查是否已创建任何新文件。

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? 有没有更好的办法?

Why don't you try with FileObservers 为什么不尝试使用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 参考仅针对文件收到FileObserver CREATE或DELETE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM