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

2

6

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
Afterword answered 15/2, 2013 at 22:9 Comment(5)
Does your code have write access to the file? Is the file in open status?Bowel
Are you currently reading 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. Make sure you call exists() on your File Object before you try to change it to ensure you actually have a valid file.Bowel
It is a particular bad idea to work directly under C:\ because a) in more recent Windows Versions you're not supposed to touch/change files and directories there b) you might accidentaly overwrite or remove OS files (like the boot configuration file).Py
Okay thanks! I acessed the file before without closing. I have to .close() it there. But if I open a file with "new file(...)", like in the code, is it also necessary to close it? Because there it isn't declared a variabel name, too...Afterword
You must close() an open File Object before you can successfully call setLastModified(time). In the case of a new File("xxxx.txt"), if "xxxx.txt" refers to an actual file, you can call setLastModified(time) and it will change the time of the file.Bowel
B
2

From my comments from earlier, follow these checks:

  1. Does your code have write access to the file?
  2. Is the file in open status?
  3. 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.

Bowel answered 15/2, 2013 at 22:34 Comment(0)
S
2

Tested your code on my local and it works... I changed the modified date of very old file on my system...

-See if file is being used somewhere else... -check if you have permissions on file

import java.io.File;
import java.io.IOException;
import java.util.Date;

class Test
{
    private class flocalClass
    {

        public String date;
        public String location="c:/Test/cascade.xyz";

    }
    public static void main (String[]args) throws IOException
    {
        flocalClass fLocal = new Test().new flocalClass();
        fLocal.date = Long.toString(new Date().getTime());
        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)));
    }
}
Stoffel answered 15/2, 2013 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.