在我的应用程序中,我使用以下方法读取文件,
public void readFIleData(String path) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(path));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println("Data : "+sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我还使用以下方法获取文件的上次访问时间和上次修改时间,
public void getFIleInfo(String path) {
Path file = Paths.get(path);
try {
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
FileTime accessTime = attrs.lastAccessTime();
System.out.println("accessTime : "+accessTime.toMillis());
FileTime modifiedTime = attrs.lastModifiedTime();
System.out.println("modifiedTime : "+modifiedTime.toMillis());
} catch (IOException e) {
e.printStackTrace();
}
}
我按以下顺序运行了上述方法,
1-> getFIleInfo()
2-> readFIleData()
3-> getFIleInfo()
我得到以下输出,
accessTime : 1462943491685
modifiedTime : 1462943925846
Data : erteuyuittdgfdfghjkhw5643rtrr66664fdghf
accessTime : 1462943491685
modifiedTime : 1462943925846
这里是字符串格式的输出时间,
accessTime : 2016-05-11T05:11:31.685881Z
modifiedTime : 2016-05-11T07:39:28.237884Z
Data : erteuyuittdgfdfghjkhw5643rtrr66LE229F1HBQ664fdghf
accessTime : 2016-05-11T05:11:31.685881Z
modifiedTime : 2016-05-11T07:39:28.237884Z
我对这个输出有疑问,因为访问时间与读取文件数据之前相同。有人可以向我解释一下 java 中上次访问时间和上次修改时间的实际含义吗?
最佳答案
首先,让我们关注一下这些东西的含义。
访问 - 最后一次读取文件的时间,即最后一次访问文件数据的时间。
Modify - 最后一次修改文件(内容已被修改),即最后修改文件数据的时间。
更改 - 文件的元数据最后一次更改(例如权限),即文件状态最后一次更改的时间。
编辑。
访问时间正在更改。 我建议你使用 Thread.sleep(100)
或其他东西,然后看看这个问题是否仍然存在。
如果是这样,罪魁祸首将是您正在运行的操作系统,因为 Java 只是从文件系统中读取。 @Serge Ballesta的评论应该让您了解 Windows NTFS 有一个选项,可以出于性能原因禁用将对文件属性所做的每个更改写回硬盘驱动器。实际上还有更多。
来自 [ docs ],
NTFS delays updates to the last access time for a file by up to one hour after the last access. NTFS also permits last access time updates to be disabled. Last access time is not updated on NTFS volumes by default.
以下是在 mac os x 上运行脚本的一些数据。
calling getFileInfo() at: 11.4.2016 3:13:08:738
accessTime : 11.4.2016 3:12:53:0
modifiedTime : 29.10.2015 1:49:14:0
--------------------
sleeping for 100ms
--------------------
calling readFIleData() at: 11.4.2016 3:13:08:873
--------------------
sleeping for 100ms
--------------------
re-calling getFileInfo() at: 11.4.2016 3:13:08:977
accessTime : 11.4.2016 3:13:08:0 <---- READING FILE CHANGES ACCESS TIME
modifiedTime : 29.10.2015 1:49:14:0
--------------------
sleeping for 100ms
--------------------
re-calling getFileInfo() at: 11.4.2016 3:13:09:81
accessTime : 11.4.2016 3:13:08:0 <---- READING FILE ATTRIBUTES DOES NOT CHANGE ACCESS TIME
modifiedTime : 29.10.2015 1:49:14:0
为了提高清晰度,您可以将您拥有的毫秒数转换为更具可读性的内容。以下代码片段将对此进行详细说明。
long accessTimeSinceEpoch = Files.readAttributes(file, BasicFileAttributes.class).lastAccessTime().toMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(accessTimeSinceEpoch);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR);
int mMin = calendar.get(Calendar.MINUTE);
int mSec = calendar.get(Calendar.SECOND);
int mMilisec = calendar.get(Calendar.MILLISECOND);
String st = mDay + "." + mMonth + "." + mYear + " " + mHour + ":" + mMin + ":" + mSec + ":" + mMilisec;
关于java - Java 中的文件上次访问时间和上次修改时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155733/