Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > File.setLastModified() question

Reply
Thread Tools

File.setLastModified() question

 
 
PilotYid
Guest
Posts: n/a
 
      09-29-2005
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.cur rentTimeMillis()));
System.out.println("After=" + file.lastModified());

 
Reply With Quote
 
 
 
 
HalcyonWild
Guest
Posts: n/a
 
      09-29-2005

PilotYid wrote:
>
> 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.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-30-2005
On 29 Sep 2005 13:28:36 -0700, "PilotYid" <(E-Mail Removed)> wrote
or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
HalcyonWild
Guest
Posts: n/a
 
      09-30-2005

HalcyonWild wrote:
> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-30-2005
On 30 Sep 2005 02:40:35 -0700, "HalcyonWild" <(E-Mail Removed)>
wrote or quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
PilotYid
Guest
Posts: n/a
 
      09-30-2005
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-30-2005
On 30 Sep 2005 06:32:06 -0700, "PilotYid" <(E-Mail Removed)> wrote
or quoted :

>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.

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments