在java中查看目录中的更改时避免检测不完整的文件 [英] Avoid detecting incomplete files when watching a directory for changes in java

查看:91
本文介绍了在java中查看目录中的更改时避免检测不完整的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看传入文件的目录(使用 FileAlterationObserver这个例子实现了FileAlterationListener {
public void prepare(){
File directory = new File(/ tmp / incoming);
FileAlterationObserver observer = new FileAlterationObserver(directory);
observer.addListener(this);
FileAlterationMonitor monitor = new FileAlterationMonitor(10);
monitor.addObserver(observer);
monitor.start();
// ...
}

public void handleFile(File f){
// FIXME:当写入
//创建文件已经完成,而不是
}

public void onFileCreate(File f){
handleFile(f);
}

public void onFileChange(File f){
handleFile(f);






这些文件由我拥有的进程编写没有控制。



这个代码的问题是当我最初创建文件时,我的回调被触发。我需要它来触发文件已被更改,并写入文件已完成。 (也许通过检测文件停止更改)



最好的方法是什么?

解决方案

我有一个类似的问题。起初,我以为我可以使用FileWatcher服务,但它不适用于远程卷,我必须通过网络驱动器监视传入的文件。



然后我想我可以在一段时间内简单地监视文件大小的变化,并在文件大小稳定之后考虑文件的完成(如fmucar建议的那样)。但是我发现,在一些大文件的情况下,主机系统会报告正在复制的文件的大小,而不是写入磁盘的字节数。这当然使文件看起来很稳定,而我的探测器会在文件正在写入的过程中捕获这个文件。



我最终能够获得显示器通过使用FileInputStream异常工作,即使当文件位于网络驱动器上时,该异常也可以很好地检测文件是否正在写入。

  long oldSize = 0L; 
long newSize = 1L;
布尔fileIsOpen = true; ((newSize> oldSize)|| fileIsOpen){
oldSize = this.thread_currentFile.length();


尝试{
Thread.sleep(2000);
} catch(InterruptedException e){
e.printStackTrace();
}
newSize = this.thread_currentFile.length();

尝试{
new FileInputStream(this.thread_currentFile);
fileIsOpen = false;
catch(Exception e){}
}

System.out.println(New file:+ this.thread_currentFile.toString());


I am watching a directory for incoming files (using FileAlterationObserver from apache commons).

class Example implements FileAlterationListener {
    public void prepare() {
        File directory = new File("/tmp/incoming");
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        observer.addListener(this);
        FileAlterationMonitor monitor = new FileAlterationMonitor(10);
        monitor.addObserver(observer);
        monitor.start();
        // ...
    }

    public void handleFile(File f) {
        // FIXME: this should be called when the writes that 
        // created the file have completed, not before
    }

    public void onFileCreate(File f) {
        handleFile(f);
    }

    public void onFileChange(File f) {
        handleFile(f);
    }
}

The files are written in place by processes that I have no control over.

The problem I have with that code is that my callback is triggered when the File is initially created. I need it to trigger when the file has been changed and the write to the file has completed. (maybe by detecting when the file stopped changing)

What's the best way to do that?

解决方案

I had a similar problem. At first I thought I could use the FileWatcher service, but it doesn't work on remote volumes, and I had to monitor incoming files via a network mounted drive.

Then I thought I could simply monitor the change in file size over a period of time and consider the file done once the file size had stabilized (as fmucar suggested). But I found that in some instances on large files, the hosting system would report the full size of the file it was copying, rather than the number of bytes it had written to disk. This of course made the file appear stable, and my detector would catch the file while it was still in the process of being written.

I eventually was able to get the monitor to work, by employing a FileInputStream exception, which worked wonderfully in detecting whether a file was being written to, even when the file was on a network mounted drive.

      long oldSize = 0L;
      long newSize = 1L;
      boolean fileIsOpen = true;

      while((newSize > oldSize) || fileIsOpen){
          oldSize = this.thread_currentFile.length();
          try {
            Thread.sleep(2000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          newSize = this.thread_currentFile.length();

          try{
              new FileInputStream(this.thread_currentFile);
              fileIsOpen = false;
          }catch(Exception e){}
      }

      System.out.println("New file: " + this.thread_currentFile.toString());

这篇关于在java中查看目录中的更改时避免检测不完整的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆