0

我正在制作一个使用 stable 监视文件读出更改的服务RecursiveFileObserver,这个文件观察者将被分配来观察文件 Modifications ( FileObserver.MODIFY) ...我希望这个观察者做onEvent的是将文件的值更改为它之前的值改变了..所以循环是:

服务启动和观察者开始观看 --> 文件由 X 更改 --> 我的观察者将其更改为被 X 更改之前的任何内容...

所以我只需要一种方法来读取文件在被 X 更改之前的值。

那么有人可以帮助我吗,甚至有可能吗?

这是我正在尝试做的一个片段......

    @Override
    public void onEvent(int event, String path) {
        // TODO Auto-generated method stub
        super.onEvent(event, path);

        if(event != MODIFY){
            return;
        }

        if (CoresManagement.isAccessAllowed()){ //If the access is allowed , doesn't do anything ...        
            return;
        }

        //Here is where I want to do what I said above

            if (path == "le Wild Location"){
              modifyFile("le Wild Location" , "Here is the value I want to know , the old value " );
            }

如果您发现任何不清楚的地方,我可以澄清一下!

4

1 回答 1

0

what you can do it to implementa a FileObserver

fileObserver = new FileObserver(pathOfFile) {

 @Override
 public void onEvent(int event, String file) {
    // on any change in the file you can replace the old file. 
    //basically keep the backup file some where or in the buffer and replace with this file if there is any change. 
 }
 };
 fileObserver.startWatching(); 

to replace file.

FileChannel origFile = null;
FileChannel modFile = null;
long cnt = 0;
long size = 0;

try {
    origFile = new FileInputStream(sourceFile).getChannel();
    modFile = new FileOutputStream(destFile).getChannel();

    size = origFile.size();              
    while((cnt += modFile.transferFrom(origFile, cnt, size-cnt))<size);
}
finally {
    if(origFile != null) {
        origFile.close();
    }
    if(modFile != null) {
        modFile.close();
    }
}
于 2013-06-25T11:44:48.823 回答