如何知道文件复制是“进行中”/完成在Java(1.6) [英] How to know whether a file copying is 'in progress'/complete in java (1.6)
问题描述
我正在用java(1.6)编写一个目录监视工具,使用 lastModified
long值作为改变的指示,以一定的时间间隔进行轮询。我发现,当我的轮询时间间隔很小(秒),复制的文件是大的,然后在文件复制的实际完成之前发生更改事件。
我想知道是否有一种方式可以找到文件的状态,如传输中,完成等。
环境:Java 1.6;预计在Windows和Linux上工作。
过去有两种方法是平台不可知的。 / p>
基本上,无论是放置一个文件 file.txt
,当它完成时,还会放置一个小文件(可能是零字节),称为文件.txt.marker
(例如)。
这样,监视工具只是查找标记文件出现,当它它知道真正的文件是完整的。它可以处理真正的文件,并删除标记。
监视器程序等待,直到文件没有改变N秒(其中N被合理地保证足够大,文件将完成)。
例如,如果文件大小在60秒内没有改变,那么很可能就完成了。
没有考虑完成文件之间的平衡因为没有任何活动,等待完成后才能开始处理。对于本地复制而言,这比FTP更少。
I am writing a directory monitoring utility in java(1.6) using polling at certain intervals using lastModified
long value as the indication of change. I found that when my polling interval is small (seconds) and the copied file is big then the change event is fired before the actual completion of file copying.
I would like to know whether there is a way I can find the status of file like in transit, complete etc.
Environments: Java 1.6; expected to work on windows and linux.
There are two approaches I've used in the past which are platform agnostic.
1/ This was for FTP transfers where I controlled what was put, so it may not be directly relevant.
Basically, whatever is putting a file file.txt
will, when it's finished, also put a small (probably zero-byte) dummy file called file.txt.marker
(for example).
That way, the monitoring tool just looks for the marker file to appear and, when it does, it knows the real file is complete. It can then process the real file and delete the marker.
2/ An unchanged duration.
Have your monitor program wait until the file is unchanged for N seconds (where N is reasonably guaranteed to be large enough that the file will be finished).
For example, if the file size hasn't changed in 60 seconds, there's a good chance it's finished.
There's a balancing act between not thinking the file is finished just because there's no activity on it, and the wait once it is finished before you can start processing it. This is less of a problem for local copying than FTP.
这篇关于如何知道文件复制是“进行中”/完成在Java(1.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!