File.setLastModified() question

P

PilotYid

Does a file have to be closed to use setLastModified() to change its
modification date?

I have a RandomAccessFile that I am writing to, but the modified time
doesn't change when it is written to. I would like use
setLastModified() to update the time, but it returns false unless I
close the RandomAccessFile first. For example, the code below only
works if I uncomment the close() below.
Does anyone know if there is a way to update the modified time without
closing the file? I am running this on WinXP.

File file = new File("c:\\work\\", "FILETEST.txt");
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
//raf.close();
System.out.println("Before=" + file.lastModified());
Thread.sleep(1000);
System.out.println(file.setLastModified(System.currentTimeMillis()));
System.out.println("After=" + file.lastModified());
 
H

HalcyonWild

PilotYid said:
I have a RandomAccessFile that I am writing to, but the modified time
doesn't change when it is written to.

Try refreshing My computer, while the Thread is in sleep state.

As far as I know, if a process has a file open, no other process can
modify it. So if you java class has the file open, the OS process
cannot modify the modified date on the file. As a check, you can modify
the file multiple times between 5 second sleep intervals.

modifyFile(file, textToAppend);
close file
sleep code here
modifyFile(file, textToAppend);
close file
sleep code here
modifyFile(file, textToAppend);
close file

See if during sleep intervals, after closing the file, the date is
updated.
 
R

Roedy Green

Does a file have to be closed to use setLastModified() to change its
modification date?

I think not (Try it to be sure. It won't explode.), but logically it
makes no sense to use it other than after closing since close has a
built in setLastModified( now ) (as part of the OS most likely). Any
setLastModified you did would be soon overwritten.
 
H

HalcyonWild

HalcyonWild said:
As far as I know, if a process has a file open, no other process can
modify it. So if you java class has the file open, the OS process
cannot modify the modified date on the file.

What I meant is file is in read / write mode. Of course, you can open
two notepad instances, and play around with the files. I guess, notepad
doesnt keep the file open all the time. It opens, does the changes, and
closes the file when you Ctrl-S. Is that true.
 
R

Roedy Green

It opens, does the changes, and
closes the file when you Ctrl-S. Is that true.

Most editors don't lock the file except during the save. Word is an
exception.
 
P

PilotYid

Well, the code I posted does not seem to update the modified time
unless the file is closed. I have a log file that a process is
periodically writing to and I would like to be able to tell when the
file is updated by looking at the modified time. Writing to the file
does not seem to do this with a RandomAccessFile, and using
setLastModified doesnt seem to work either. Any other ideas? Is this a
bug?

Thanks again
Aaron
 
R

Roedy Green

Well, the code I posted does not seem to update the modified time
unless the file is closed.

If you are trying to update the file time without closing it, you
need at the OS level a COMMIT. I don't know if a flush generates one.
It is worth a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top