determining when a file has been moved/changed

Discussion in 'Java' started by Rob Baxter, Jul 28, 2004.

  1. Rob Baxter

    Rob Baxter Guest

    I'm trying to implement a java program which will mirror the
    functionality of the Unix utility tail -f (ie continually read the end
    of a file). I have a fairly simple solution using FileInputStream &
    bufferedReader in an infinite loop where I call Thread.sleep(1000)
    every time the read returns null (indicating the end of the
    FileInputStream has been reached).

    The problem I am having is that I am using this to read the end of a
    syslog log file and the files are logrotated every hour. When the
    logrotate process renames the file I have opened my program continues
    to read from the same file which is no longer being written to by
    syslog. What I am looking for is a way to either be notified or be
    able to determine when another process has moved/renamed a file I am
    reading from. I've tried getting the file descriptor from the
    FileInputStream and calling it's valid() method but it always returns
    true even if I delete the file after it has been opened.

    I know I can rig something up using the filesize to check if the file
    has been rotated, but I was wondering if there was a more elegant
    solution. any hints or advice are greatly appreciated (also if anyone
    knows of a version of tail which doesn't suffer from this same problem
    I'd be interested in seeing that also)

    TIA,
    -rob
    Rob Baxter, Jul 28, 2004
    #1
    1. Advertising

  2. I doubt you'll find any tail utility that does not have this "problem"
    on Unixoid file systems. The real problem is that these file system have
    a concept of "file" that goes against your expectations, namely in that
    a file and a path are two completely separate things. There is an extra
    layer (inode layer) that abstracts files from directory entries, and
    a file can be referred by several completely different names in different
    directories, or even keep existing when there are no directory entries
    pointing to it at all.
    Michael Borgwardt, Jul 28, 2004
    #2
    1. Advertising

  3. Rob Baxter

    Jacob Guest

    Rob Baxter wrote:
    > I'm trying to implement a java program which will mirror the
    > functionality of the Unix utility tail -f (ie continually read the end
    > of a file). I have a fairly simple solution using FileInputStream &
    > bufferedReader in an infinite loop where I call Thread.sleep(1000)
    > every time the read returns null (indicating the end of the
    > FileInputStream has been reached).
    >
    > The problem I am having is that I am using this to read the end of a
    > syslog log file and the files are logrotated every hour. When the
    > logrotate process renames the file I have opened my program continues
    > to read from the same file which is no longer being written to by
    > syslog. What I am looking for is a way to either be notified or be
    > able to determine when another process has moved/renamed a file I am
    > reading from. I've tried getting the file descriptor from the
    > FileInputStream and calling it's valid() method but it always returns
    > true even if I delete the file after it has been opened.
    >
    > I know I can rig something up using the filesize to check if the file
    > has been rotated, but I was wondering if there was a more elegant
    > solution. any hints or advice are greatly appreciated (also if anyone
    > knows of a version of tail which doesn't suffer from this same problem
    > I'd be interested in seeing that also)


    The FileMonitor class @ http://geosoft.no/software/index.html
    might help you out.

    If you know the name of the files to monitor, listen for them;
    You'll be notified when they're created, modified or deleted.

    If you don't know their names, listen at the directory instead.
    When it is modified, files have been accessed.
    Jacob, Jul 28, 2004
    #3
  4. Rob Baxter

    Oscar kind Guest

    Michael Borgwardt <> wrote:
    > I doubt you'll find any tail utility that does not have this "problem"
    > on Unixoid file systems. The real problem is that these file system have
    > a concept of "file" that goes against your expectations, namely in that
    > a file and a path are two completely separate things. There is an extra
    > layer (inode layer) that abstracts files from directory entries, and
    > a file can be referred by several completely different names in different
    > directories, or even keep existing when there are no directory entries
    > pointing to it at all.


    Absolutely correct.

    OP: You need to poll the last changed date (a timestamp) using the file path
    (and hence a File object), rather than waiting if there is data available
    on the InputStream you're reading from.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jul 28, 2004
    #4
    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. Dave Rudolf
    Replies:
    3
    Views:
    11,211
    Esmond Pitt
    Dec 31, 2003
  2. Marco Krechting

    NEWBIE: check if page has been changed

    Marco Krechting, Jan 7, 2005, in forum: Java
    Replies:
    2
    Views:
    755
    Andrew Thompson
    Jan 7, 2005
  3. BigMan
    Replies:
    8
    Views:
    354
    BigMan
    Feb 17, 2005
  4. Robert Potthast
    Replies:
    0
    Views:
    293
    Robert Potthast
    Aug 31, 2005
  5. nomad
    Replies:
    0
    Views:
    271
    nomad
    Aug 7, 2008
Loading...

Share This Page