file.lastModified()是从来没有什么被设置file.setLastModified() [英] file.lastModified() is never what was set with file.setLastModified()

查看:947
本文介绍了file.lastModified()是从来没有什么被设置file.setLastModified()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,米利斯设置和读取在Android 2.3.4上的Nexus One。这是code:

I do have a problem with millis set and read on Android 2.3.4 on a Nexus One. 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();
}

在这个小测试我写1310198774但是从上次更改时间()返回的结果是1310199771000。

In this small test I write 1310198774 but the result that is returned from lastModified() is 1310199771000.

即使我砍尾000有几分钟的差别。

Even if I cut the trailing "000" there's a difference of several minutes.

我需要一个互联网服务和Android设备之间同步文件。的lastmodification米利斯是由该服务所发送的数据的一部分。我设置米利斯到创建/复制的文件和文件夹,以检查文件/文件夹需要被改写。

I need to sync files between a webservice and the Android device. The lastmodification millis are part of the data sent by this service. I do set the millis to the created/copied files and folders to check if the file/folder needs to be overwritten.

一切工作,但被从文件系统返回的米利斯从已设置的值不同。

Everything is working BUT the millis that are returned from the filesystem are different from the values that were set.

我是pretty的肯定有什么毛病我的code - 但我不能找到它。

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

在预先感谢。 HJW

Many thanks in advance. HJW

推荐答案

所以,也许我失去了一些东西,但我看到了一些问题,您的code以上。您的具体问题可能是由于(如@JB提到的)到Android问题,但留给后人,我想我会提供一个答案。

So maybe I'm missing something but I see some problems with your code above. Your specific problem may be due (as @JB mentioned) to Android issues but for posterity, I thought I'd provide an answer.

首先, File.setLastModified()需要以毫秒为单位的时间。这里是的javadoc 。你似乎试图将其设置为秒。所以,你的code应该是这样的:

First off, File.setLastModified() takes the time in milliseconds. Here are the javadocs. You seem to be trying to set it in seconds. So your code should be something like:

fileFolder.setLastModified(1310198774000L);

正如的javadoc,许多文件系统仅支持秒粒度的最后修改时间。所以,如果你需要看到相同的修改时间的文件,那么你应该做类似如下:

As mentioned in the javadocs, many filesystems only support seconds granularity for last-modification time. So if you need to see the same modification time in a file then you should do something like the following:

private void changeModificationFile(File file, long time) {
    // round the value down to the nearest second
    file.setLastModified((time / 1000) * 1000);
}

这篇关于file.lastModified()是从来没有什么被设置file.setLastModified()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆