This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Jan 8, 2008 12:43 PM by 807603 RSS

Read a file without changing its access time ?

807603 Newbie
Currently Being Moderated
1) Is it possible to read a file using a fileInputStream but without updating its Last Access Time ?

2) Or maybe someway to first get the Last Access Time, then read the file and then change the access time of that file back to what it was before the read ?

3) Is it possible to do this using any other programming language that you might know of (In the windows environment)

4) Does anyone know how to read a file by disabling access time updates on windows ?

Thanks in advance. :))
  • 1. Re: Read a file without changing its access time ?
    796440 Guru
    Currently Being Moderated
    And you want to do this because...?
  • 2. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    its just that for some users of my program it is important to maintain the access times of the files.
  • 3. Re: Read a file without changing its access time ?
    800241 Newbie
    Currently Being Moderated
    what exactly is your program ?
  • 4. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    How about:

    File file=new File("");
    long x1= file.lastModified();

    ((( read the file))))

    file.setLastModified(x1);
  • 5. Re: Read a file without changing its access time ?
    800241 Newbie
    Currently Being Moderated
    How about:

    File file=new File("");
    long x1= file.lastModified();

    ((( read the file))))

    file.setLastModified(x1);
    last modified != last accessed.
  • 6. Re: Read a file without changing its access time ?
    EJP Guru
    Currently Being Moderated
    its just that for some users of my program it is
    important to maintain the access times of the files.
    But that's exactly what's happening. The access times of the files are being maintained correctly. The question is why do you not want tha to happen?
  • 7. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    I guess there is no way in java to get and set access time of a file.

    In that case, I thought I would invoke a perl script from within my java program to get the job done. However, for that purpose the users will need to have perl installed on their machine, am I right ???

    If so, then which other programming or scripting language can I use (on windows) so that users do not need to install anything else ?
  • 8. Re: Read a file without changing its access time ?
    796447 Newbie
    Currently Being Moderated
    I guess there is no way in java to get and set access
    time of a file.

    In that case, I thought I would invoke a perl script
    from within my java program to get the job done.
    However, for that purpose the users will need to have
    perl installed on their machine, am I right ???

    If so, then which other programming or scripting
    language can I use (on windows) so that users do not
    need to install anything else ?
    I do not believe there is an exposed API (at least not in Windows) which allows one to set the last accessed time of a file. Obviously at least the OS can reset it (as it is doing when you open the file thru any means), but I doubt this is exposed for applications (not even in C, C++ or possibly even lower-level Assembler) to monkey with.

    Nor should it be monkeyed with. You are apparently designing an ill-designed application which should not be using last-accessed time as a control in the first place.
  • 9. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    Well there is a perl function utime(atime,mtime) which allows you to set the access and modification time for files.

    C on unix has utime too..

    C on windows has setFileTime()

    setFileTime:
    Sets the date and time that the specified file or directory was created, last accessed, or last modified.
  • 10. Re: Read a file without changing its access time ?
    EJP Guru
    Currently Being Moderated
    You still haven't told us why you want to do this.

    If you want to update the last-accessed time of a file to 'now', just open it and close it. If you want to update the last-write time of a file to 'now', open it, read the first byte, write it back, and close it (that's what 'touch' does, or did).

    If you want to set the last-accessed time backwards to some previous time, I would want to know why.
  • 11. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    You still haven't told us why you want to do this.
    Probably trying to write a spyware program.
  • 12. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    one reason to want to preserve a file accessed date is for legal discovery purposes. if your program opens files for the purpose of indexing their content, and through that indexing you determine that the file needs to be preserved exactly as it is (was), you will need to reset the 'last access' time to preserve that file. this is, most likely, not what the OP had in mind, but i just wanted to point out that there are valid reasons for wanting to do this.
  • 13. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    A missing function in java is to be able to set and get the access time (atime) and at the moment it is a real pain with (SEC) regulatory applications written in java because of this.

    The reason is that the "SnapLock" interface for regulatory storage uses the access time to set the retention of a file. It is not just NetApp that uses it and even Sun has it in WormFS.

    The way it works is that when an application has written a file to the "archive area" it sets an atime in the future for until when a file will be immutable (WORMed) and then sets the read only flag (windows) or remove write rights to the file (unix). When that is done it is only possible to extent the atime to allow extension of the retention. When the atime is reached the file can be deleted (but still not modified).

    So, a long story short, if we could get the possibility to set the atime on files natively in java then that would be fantastic!
  • 14. Re: Read a file without changing its access time ?
    807603 Newbie
    Currently Being Moderated
    Modifying a file's last accessed setting to manage how your app will handle the file is bad practice (in this case the date the file will be "WORMed"). You're removing a valid value set by the OS for your application's specific needs. Your app should manage its own data about the files it is managing internally, not use the files themselves for this. If you must use the file, why this field? Why not alter created date, or just put the date in the file itself, or change the name of the file to add that date. All equally bad solutions to this problem.
1 2 Previous Next