java – file. Lastmodified() has never been set with file setLastModified()

My android 2.3 on Nexus One There is no doubt about setting and reading on 4 This is the code:

File fileFolder = new File(Environment.getExternalStorageDirectory(),appName + "/"
    + URLDecoder.decode(folder.getUrl()));
if (fileFolder != null && !fileFolder.exists()) {
  fileFolder.setLastModified(1310198774);
  fileFolder.mkdirs();
  fileFolder.setLastModified(1310198774);
}

if (fileFolder != null && fileFolder.exists()) {
  long l = fileFolder.lastModified();
}

In this small test, I wrote 1310198774, but the result returned from LastModified () is 1310199771000

Even if I cut off the trailing "000" for almost a few minutes

I need to synchronize files between web service and Android devices The last modification is part of the data sent by the service I set millis as the files and folders created / copied to check whether the files / folders need to be overwritten

Everything works, but the number of milliseconds returned from the file system is different from the set value

I'm sure there's something wrong with my code, but I can't find it

Thank you in advance HJW

Solution

So maybe I missed some, but I saw some problems in your code Your specific question may be due to the Android question (@ JB mentioned), but for future generations, I think I will provide an answer

First, file Setlastmodified() takes a few milliseconds This is JavaDocs You seem to be trying to set it up in a few seconds So your code should look like this:

fileFolder.setLastModified(1310198774000L);

As mentioned in Javadoc, many file systems only support the second granularity of the last modification time Therefore, if you need to see the same modification time in the file, you should do the following:

private void changeModificationFile(File file,long time) {
    // round the value down to the nearest second
    file.setLastModified((time / 1000) * 1000);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>