更改文件的最后修改时间而不关闭它
仅当文件关闭时,文件的最后修改时间才会更改。
public class Main {
public static void main(String[] args) throws IOException {
File f = new File("xyz.txt");
FileWriter fwr = new FileWriter(f);
System.out.println(f.lastModified());
fwr.write("asasdasdasd");
System.out.println(f.setLastModified(System.currentTimeMillis()));
fwr.flush();
System.out.println(f.lastModified());
fwr.close();
System.out.println(f.lastModified());
System.out.println(f.setLastModified(System.currentTimeMillis()));
}
}
现在,在我的实际程序中,打开一个文件,其中一个线程继续写入该文件。其他几个线程需要知道最后一次将数据写入文件的时间。
有没有可能的方法更新上次修改而不关闭文件? (我知道,在写入文件的线程中有一个 static
变量 - long lastWriteTime
是可以的。但只是想知道是否还有其他方法可以更改最后修改时间(未关闭文件)。
File's last modified time is changed only when the file is closed.
public class Main {
public static void main(String[] args) throws IOException {
File f = new File("xyz.txt");
FileWriter fwr = new FileWriter(f);
System.out.println(f.lastModified());
fwr.write("asasdasdasd");
System.out.println(f.setLastModified(System.currentTimeMillis()));
fwr.flush();
System.out.println(f.lastModified());
fwr.close();
System.out.println(f.lastModified());
System.out.println(f.setLastModified(System.currentTimeMillis()));
}
}
Now, in my actual program, a file is opened and one of the thread keeps writing the file. several other threads need to know when was any data last written to the file.
Is there any possible way update last modified without closing the file?
( I know, having a static
variable - long lastWriteTime
in the thread that writes the file would work. but just curious to know if there is any other way, to change the last modified time without closing the file. )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您实际想要实现的目标,两种方法之一可能是合适的:
File.setLastModified()
直接操作时间戳或f.flush()
到确保所有写入的数据都实际写入磁盘。请注意,操作系统和/或文件系统的时间戳分辨率可能比您希望的要低。例如,FAT 以 2 秒的分辨率存储这些时间戳!甚至更现代的文件系统也只能以单秒分辨率进行存储。
另请注意,上次修改时间戳的行为因操作系统而异。例如,在我的 Ubuntu 上,只有
write
/flush
修改时间戳,close()
不会!Depending on what you actually want to achieve one of two methods may be appropriate:
File.setLastModified()
to directly manipulate the timestampe orf.flush()
to ensure all data written is actually written to the disk.Note that the OS and/or the file system may have a lower resolution of that timestamp than you hope for. For example FAT stores those timestamps with a 2-second resolution! Even more modern file systems are known to store only in a single-second resolution.
Also note that the behavior of the last modified timestamp varies a lot depending on the OS. On my Ubuntu, for example, only the
write
/flush
modifies the timestamp, theclose()
doesn't!请记住,您看到的行为高度依赖于操作系统,也许还有文件系统类型。因此,java 不能也不会指定文件时间何时更新。因此,不,在java中没有可移植的方法来做到这一点。
Please keep in mind that the behaviour you see is highly dependend on OS and perhaps file system type. Hence, java can not and does not specify when file times are updated. Therefore, no , there is no portable way to do that in java.
使用 setLastModified() 怎么样?
What about using setLastModified()?