检查文件是否完整写入 [英] Checking if file is completely written

查看:133
本文介绍了检查文件是否完整写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在处理那些在源码目录中处理文件的项目。有一个Java进程正在查找指定的目录,并尝试读取和处理存在的文件。文件退出较大,由其他第三方进程更新。问题是如何检查文件是否被完整写入?我试图使用 file.length(),但看起来即使写入过程还没有完成,它返回实际大小。我有一种感觉,解决方案应该是平台依赖的。任何帮助,将不胜感激。

更新:
这个问题是没有真正不同的重复,但它有一个工作代码片段的高度评价的答案。

解决方案

生产者进程在写完文件时是否关闭文件?如果是这样的话,如果生产者进程仍在生成,试图用排它锁打开消费者进程中的文件将失败。


Currently I'm working on the project that does processing files from source directory in one of it's routines. There's a Java process that's looking for specified directory and tries to read and process files if they exist. Files are quit large and updates by other thirdparty process. The question is how can I check if the file is completely written? I'm trying to use file.length() but looks like even if writing process hasn't been completed it returns actual size. I have a feeling that solution should be platform dependent. Any help would be appreciated.

UPDATE: This question is not really different from the duplicate but it has an answer with working code snippet that is highly rated.

解决方案

Does the producer process close the file when its finished writing? If so, trying to open the file in the consumer process with an exclusive lock will fail if the producer process is still producing.

这篇关于检查文件是否完整写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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