file.lastModified()从来没有使用file.setLastModified()设置 [英] file.lastModified() is never what was set with file.setLastModified()

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

问题描述

我确实在Nexus One上的Android 2.3.4上设置和阅读了millis的问题。这是代码:

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,但返回的结果是lastModified )是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.

我需要在Web服务和Android设备之间同步文件。最后一次修改是该服务发送的数据的一部分。我确定将millis设置为创建/复制的文件和文件夹以检查文件/文件夹是否需要被覆盖。

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.

我很确定我的代码有问题,但我找不到。

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

非常感谢提前。
HJW

Many thanks in advance. HJW

推荐答案

所以也许我错过了一些东西,但是我看到你的代码上面有一些问题。您的具体问题可能是由于(@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()花费时间在毫秒。以下是 javadocs 。你似乎试图在几秒钟内设置它。所以你的代码应该是这样的:

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天全站免登陆