简体   繁体   English

我可能会对文件执行哪些操作,从而导致file.lastModified()调用更改其值?

[英]What operations might I might perform on a file that would cause the file.lastModified() call to change it's value?

I'm wondering what types of programmatic behaviors count as "modifying" a file in java. 我想知道什么类型的编程行为算作“修改” java中的文件。 That is, what operations which i might perform on a file would cause the file.lastModified() call to change it's value? 也就是说,我可能对文件执行的哪些操作将导致file.lastModified()调用更改其值?

IE Opening? IE浏览器开放? Reading? 读? Writing? 写作? Copying? 复制吗? Writing is an obvious trigger, but the others aren't so obvious. 写作是显而易见的触发因素,但其他因素却不是那么明显。 I've done some looking around online and not seen anything immediate. 我已经在网上四处张望,没有立即看到任何东西。

Clarification: I'm not tyring to set the last modified time, i'm just attempting to determine what programmatic behaviors which a java program might engage in would cause this value to update. 澄清:我不打算设置上次修改时间,我只是试图确定Java程序可能参与的编程行为会导致此值更新。 Obviously writing to a file would do this. 显然,写入文件可以做到这一点。 But are there other cases which do cause this? 但是还有其他情况会导致这种情况吗?

Reading depends on opening, Copying depends on reading. 阅读取决于打开,复制取决于阅读。 Writing is the only trigger to set the Last Modified stamp. 书写是设置“上次修改”标记的唯一触发器。 Once you open a file, in most operating systems, you need to specify what modes you want to open the file with. 在大多数操作系统中,打开文件后,需要指定打开文件的方式。 eg rw or r or w or a (append) etc. (implementation / os dependant). 例如rwrwa (附加)等。(实施/取决于操作系统)。 Since a includes w , w is the only operation that will set the Last Modified stamp. 由于a包含w ,因此w是唯一可以设置“上次修改”戳记的操作。

Java classes (such as FileInputStream ) will open the file specifying r or "read" - so that won't trigger the LastModified stamp. Java类(例如FileInputStream )将打开指定r或“ read”的文件-这样就不会触发LastModified标记。 Java has distinct classes for Readers and Writers - Input and Output. Java对于读写器有不同的类-输入和输出。 The input/reader classes ( Reader , InputStream ) will not set the LastModified stamp. 输入/阅读器类( ReaderInputStream )将不会设置LastModified标记。 However, a simple Open-For-Write (without actually writing) will most likely also trigger the LastModified stamp, ie: 但是,一个简单的Open-For-Write(无需实际写操作)很可能也会触发LastModified戳记,即:

new FileOutputStream( new File( "path/to/file.extension" ) );

will most likely trigger the LastModified set. 将最有可能触发LastModified集。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM