简体   繁体   English

获取文件的上次修改日期

[英]Getting a File's Last Modified Date

I'm using this to see if a file already exists and get its timestamp: 我正在使用它来查看文件是否已存在并获取其时间戳:

File file = new File(getResources().getString(R.string.file_name));

if (file.exists()) {
  Date lastModified = new Date(file.lastModified());
}

Even though I can see that this file does indeed exist using the Context.fileList() method, the code above always says it doesn't. 即使我可以使用Context.fileList()方法看到这个文件确实存在,但上面的代码总是说它没有。

How can I get a file's last modified date? 如何获取文件的上次修改日期?

I take it your problem is that file.exists() fails, the issue with the modified date has nothing to do with it. 我认为你的问题是file.exists()失败了,修改日期的问题与它无关。

I'd venture that the path you're using is local to your application? 我冒昧地说你正在使用的路径是你的应用程序的本地路径? You'll need to use absolute paths when using File . 使用File时,您需要使用绝对路径。

Use this for the path 将此用作路径

if u send invalid path then u will always get 0 or the 1970 something! 如果你发送无效路径,你将永远得到0或1970年的东西! (because the Google set The start Date to that date :) ) (因为Google将开始日期设置为该日期:))

File file = new File(this.getFilesDir().getAbsolutePath() + "/file1.jpg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM