Here is the code that I started with:
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);
And the result that I saw was:
renamed: true
time changed: false
But when I tried:
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);
Everything seemed to be fine with this output:
renamed: true
time changed: true
Why does the second approach work, but the first does not?
source
share