这是我开始使用的代码:
long modifiedTime = [some time here];
File oldFile = new File("old_name.txt");
boolean renamed = oldFile.renameTo(new File("new_name.txt");
boolean timeChanged = oldFile.setLastModified(modifiedTime);
System.out.println("renamed: " + renamed);
System.out.println("time changed: " + timeChanged);
我看到的输出是:
renamed: true
time changed: false
但是当我尝试时:
long modifiedTime = [some time here];
boolean renamed = new File("old_name.txt").renameTo(new File("new_name.txt"));
boolean timeChanged = new File("new_name.txt").setLastModified(modifiedTime);
System.out.println("renamed: " + renamed);
System.out.println("time changed: " + timeChanged);
它似乎工作正常,有这个输出:
renamed: true
time changed: true
为什么第二种方法有效,而第一种方法无效?