Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

File.setLastModified() question

Thread view: 
PilotYid - 29 Sep 2005 21:28 GMT
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());
HalcyonWild - 29 Sep 2005 21:56 GMT
> 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.
HalcyonWild - 30 Sep 2005 10:40 GMT
> 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.
Roedy Green - 30 Sep 2005 10:58 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

PilotYid - 30 Sep 2005 14:32 GMT
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
Roedy Green - 30 Sep 2005 23:07 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 30 Sep 2005 05:00 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.