java-文件lastModified与读取文件 [英] java - File lastModified vs reading the file

查看:209
本文介绍了java-文件lastModified与读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文件,并且在修改文件时需要更新Java中的值. 因此,我正在考虑使用File类的lastModified检查修改时间,如果修改,则读取文件并从文件更新单个属性.

I am using a file and need to update value in java when file is modified. So, I am thinking to check modified time using lastModified of File class, and if modified read the file and update single property from the file.

我的疑问是,lastModified是否重于从文件中读取单个属性/读取整个文件.因为我的测试结果显示几乎相同的结果.

My doubt is, is lastModified as heavy as reading single property from the file/reading whole file. Because my test results are showing almost same results.

因此,每次读取文件并从文件中更新属性是更好的选择,还是从长远来看,选择lastModified是更好的选择.

So is it better to read file and update property from the file everytime or checking lastModified is better option in long run.

注意:每隔一分钟执行一次此操作.

Note: This operation is performed every one minute.

还是有比轮询lastModified更好的选项,以检查文件是否已更改.我正在使用Java 6.

Or is there any better option than polling lastModified to check if file has changed. I am using java 6.

推荐答案

由于您使用的是Java 6,因此检查修改日期或文件内容是唯一的选择(还有另一个答案讨论使用较新的java.nio.file功能. ,并且如果您可以选择使用Java 7,则应该考虑这一点.

Because you are using Java 6, checking the modified date or file contents is your only option (there's another answer that discusses using the newer java.nio.file functionality, and if you have the option of moving to Java 7, you should really, really consider that).

要回答您的原始问题:

您没有指定文件的位置(例如,它在本地磁盘还是其他位置的服务器上)-我会以本地磁盘为例进行响应,但是如果文件在其他计算机上,则网络等待时间和netbios /dfs/whatever-network-file-system-you-使用会加剧差异.

You didn't specify the location of the file (i.e. is it on a local disk or a server somewhere else) - I'll respond assuming local disk, but if the file is on a different machine, network latencies and netbios/dfs/whatever-network-file-system-you-use will exacerbate the differences.

检查文件的修改日期涉及从磁盘读取元数据.检查文件内容需要从磁盘读取文件内容(如果文件较小,这将是一次读取操作.如果文件较大,则可能是多次读取操作).

Checking modified date on a file involves reading the meta data from disk. Checking the contents of the file require reading the file contents from disk (if the file is small, this will be one read operation. If the file is larger, it could be multiple read operations).

读取文件的内容可能涉及读/写锁定检查.一般来说,检查文件上的修改日期不需要进行读/写锁定检查(取决于文件系统, 可能仍会在元数据磁盘页面上发生一致性锁定,但通常比文件锁更轻).

Reading the content of the file will probably involve read/write lock checking. Generally speaking, checking the modified date on the file will not require read/write lock checking (depending on the file system, there may still be consistency locks occurring on the meta data disk page, but those are generally lighter weight than file locks).

如果文件经常更改(即您实际上希望它每分钟更改一次),那么检查修改日期只是开销-无论如何,您将在大多数情况下读取文件内容.如果文件不经常更改,那么修改日期检查(如果文件很大)肯定是有好处的(您必须阅读整个文件以获取信息).

If the file changes frequently (i.e. you actually expect it to change every minute), then checking the modified date is just overhead - you are going to read the file contents in most cases anyway. If the file doesn't change frequently, then there would definitely be an advantage to modified date checking if the file was large (and you had to read the entire file to get at your information).

如果文件很小,并且不经常更改,则几乎可以避免.在大多数情况下,文件内容和文件元数据已经被分页到RAM中-因此这两种操作都是对RAM中的内容进行比较有效的检查.

If the file is small, and doesn't change frequently, then it's pretty much a wash. In most cases, the file contents and the file meta data are already going to be paged into RAM - so both operations are a relatively efficient check of contents in RAM.

我个人将进行修改的日期检查只是从逻辑上讲是合理的(如果文件大小超过一个磁盘页面,它可以保护您免受性能下降的影响)-但是,如果文件频繁更改,那么我d只是读取文件内容.但实际上,任何一种方法都可以.

I personally would do the modified date check just b/c it logically makes sense (and it protects you from the performance hit if the file size ever grows above one disk page) - but if the file changes frequently, then I'd just read the file contents. But really, either way is fine.

这给我们带来了不请自来的建议:我的猜测是,在更大的事情方案中,此操作的性能并不重要.即使比现在花费了1000倍的时间,它可能仍然不会影响应用程序的主要用途/性能.因此,我的真正建议是编写代码并继续前进-不必担心它的性能,除非这成为您应用程序的瓶颈.

And that brings us to the unsolicited advice: my guess is that the performance on this operation isn't a big deal in the greater scheme of things. Even if it took 1000X longer than it does now, it probably still wouldn't impact your application's primary purpose/performance. So my real advice here is just write the code and move on - don't worry about it's performance unless this becomes a bottleneck for your application.

这篇关于java-文件lastModified与读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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