File.setLastModified() question

Discussion in 'Java' started by PilotYid, Sep 29, 2005.

  1. PilotYid

    PilotYid Guest

    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());
    PilotYid, Sep 29, 2005
    #1
    1. Advertising

  2. PilotYid

    HalcyonWild Guest

    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.
    HalcyonWild, Sep 29, 2005
    #2
    1. Advertising

  3. PilotYid

    Roedy Green Guest

    On 29 Sep 2005 13:28:36 -0700, "PilotYid" <> 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.
    Roedy Green, Sep 30, 2005
    #3
  4. PilotYid

    HalcyonWild Guest

    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.
    HalcyonWild, Sep 30, 2005
    #4
  5. PilotYid

    Roedy Green Guest

    On 30 Sep 2005 02:40:35 -0700, "HalcyonWild" <>
    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.
    Roedy Green, Sep 30, 2005
    #5
  6. PilotYid

    PilotYid Guest

    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
    PilotYid, Sep 30, 2005
    #6
  7. PilotYid

    Roedy Green Guest

    On 30 Sep 2005 06:32:06 -0700, "PilotYid" <> 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.
    Roedy Green, Sep 30, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,386
    Scott Allen
    Jun 4, 2005
  2. key9
    Replies:
    7
    Views:
    366
    Pete Becker
    Sep 13, 2006
  3. Dave
    Replies:
    1
    Views:
    962
    Juan T. Llibre
    Jun 8, 2007
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    768
    Alf P. Steinbach
    Jul 10, 2007
  5. scad
    Replies:
    4
    Views:
    934
    James Kanze
    May 28, 2009
Loading...

Share This Page