java (1.6) 如何知道文件复制是否“正在进行"/完成 [英] How to know whether a file copying is 'in progress'/complete in java (1.6)
问题描述
我正在 java(1.6) 中编写目录监视实用程序,使用 lastModified
长值作为更改指示,以特定时间间隔轮询.我发现当我的轮询间隔很小(秒)并且复制的文件很大时,在实际完成文件复制之前会触发更改事件.
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.
环境:Java 1.6;预计适用于 windows 和 linux.
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/这是我控制的 FTP 传输,所以它可能不直接相关.
1/ This was for FTP transfers where I controlled what was put, so it may not be directly relevant.
基本上,无论放置什么文件 file.txt
,当它完成时,也会放置一个名为 file.txt.marker
(例如).
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/持续时间不变.
让你的监控程序等待 N 秒,直到文件没有改变(其中 N 可以合理地保证足够大,以便文件将完成).
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).
例如,如果文件大小在 60 秒内没有改变,则很有可能它已完成.
For example, if the file size hasn't changed in 60 seconds, there's a good chance it's finished.
不认为文件已完成只是因为文件上没有活动,这与等待文件完成后才能开始处理之间存在平衡.与 FTP 相比,这对于本地复制来说不是问题.
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屋!