如何测试文件是否“完整” (完全写)用Java [英] How to test if a file is "complete" (completely written) with Java

查看:338
本文介绍了如何测试文件是否“完整” (完全写)用Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个外部进程将文件写入某个目录,并且您有一个单独的进程定期尝试从该目录中读取文件。要避免的问题是读取另一个进程当前正在写出的文件,因此它将是不完整的。当前,读取的进程使用最小文件年龄计时器检查,因此它将忽略所有文件,除非它们的上次修改日期超过XX秒。

Let's say you had an external process writing files to some directory, and you had a separate process periodically trying to read files from this directory. The problem to avoid is reading a file that the other process is currently in the middle of writing out, so it would be incomplete. Currently, the process that reads uses a minimum file age timer check, so it ignores all files unless their last modified date is more than XX seconds old.

我想知道是否有更清洁的方法来解决这个问题。如果文件类型未知(可能是多种不同的格式),是否有一些可靠的方法来检查文件头中应该在文件中的字节数,以及文件中当前确认它们匹配的字节数?

I'm wondering if there is a cleaner way to solve this problem. If the filetype is unknown (could be a number of different formats) is there some reliable way to check the file header for the number of bytes that should be in the file, vs the number of bytes currently in the file to confirm they match?

感谢您的任何想法或想法!

Thanks for any thoughts or ideas!

推荐答案

您可以使用外部标记文件。写入过程可以在开始创建文件XYZ之前创建文件XYZ.lock,并在XYZ完成后删除XYZ.lock。然后,读者可以很容易地知道,只有当相应的.lock文件不存在时,它才能认为文件是完整的。

You could use an external marker file. The writing process could create a file XYZ.lock before it starts creating file XYZ, and delete XYZ.lock after XYZ is completed. The reader would then easily know that it can consider a file complete only if the corresponding .lock file is not present.

这篇关于如何测试文件是否“完整” (完全写)用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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