Why doesn't setLastModified(time) work for this File?

date, file, java

Solution

From my comments from earlier, follow these checks:

- Does your code have write access to the file?

- Is the file in open status?

- Are you currently reading (or writing!) the file with any other application at the time you are doing this?

These are all items that might prevent you from changing the time of the file.

Create a simple plain text file with a single line of text, save it and close out of the editor. Then try using that file in your application. Make sure you call `exists()` on your `File` `Object` before you try to change the time of it to ensure you actually have a valid file.

Problem

Why is the date of the file in the following code not changed? `fLocal.location` = Existing file in C:\ `fLocal.date` = Date to set in Long ``` boolean x = new File(fLocal.location).setLastModified(Long.parseLong(fLocal.date)); System.out.println("Changed: " + x); System.out.println(new Date(new File(fLocal.location).lastModified())); System.out.println(new Date(Long.parseLong(fLocal.date))); ``` Output: ``` Changed: false Fri Feb 15 23:02:51 CET 2013 Fri Feb 15 22:49:34 CET 2013 ```

Original source