获取文件的上次修改日期

Tom*_*Tom 21 android

我正在使用它来查看文件是否已存在并获取其时间戳:

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

if (file.exists()) {
  Date lastModified = new Date(file.lastModified());
}
Run Code Online (Sandbox Code Playgroud)

即使我可以使用Context.fileList()方法看到这个文件确实存在,但上面的代码总是说它没有.

如何获取文件的上次修改日期?

Ebo*_*ike 12

我认为你的问题是file.exists()失败了,修改日期的问题与它无关.

我冒昧地说你正在使用的路径是你的应用程序的本地路径?使用时需要使用绝对路径File.

  • 这确实是一个路径问题.解决方案是使用Context.getFileStreamPath(fileName)来获取File对象.然后file.exists()和file.lastModified()方法工作正常.谢谢你的提示. (2认同)