23

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.

How can I get a file's last modified date?

2
  • 1
    What's your issue here? That file.exists() fails? Commented Nov 4, 2010 at 23:04
  • file.exists() is returning false. Whenever I call file.lastModified() it returns zero. The file name string is just a name like "myfile.txt" without any path information. Commented Nov 4, 2010 at 23:24

2 Answers 2

13

I take it your problem is that file.exists() fails, the issue with the modified date has nothing to do with it.

I'd venture that the path you're using is local to your application? You'll need to use absolute paths when using File.

Sign up to request clarification or add additional context in comments.

2 Comments

It was indeed a path problem. The solution was to use Context.getFileStreamPath(fileName) to get the File object. Then the file.exists() and file.lastModified() methods worked fine. Thanks for the tip.
Glad to help! This pathing thing is a common pitfall.
1

Use this for the path

if u send invalid path then u will always get 0 or the 1970 something! (because the Google set The start Date to that date :) )

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

1 Comment

It was actually not "the Google" who set that date but rather the nature of how unix timestamps are made - see en.wikipedia.org/wiki/Unix_time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.