我正在使用稳定的RecursiveFileObserver
制作一个监视文件读数变化的服务,这个文件观察者将被分配来观察文件修改(FileObserver.MODIFY
)...我想要这个观察者do onEvent
是将文件的值更改为更改之前的值。所以循环是:
服务开始和观察者startWatching - >文件由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 " );
}
如果您发现任何不清楚我可以澄清的话,请,
答案 0 :(得分:0)
你可以做些什么来实现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();
替换文件。
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();
}
}