How can I follow a file like “Tail -f” does in Java without holding the file open (Prevent rename/delete)

前端 未结 3 909
离开以前
离开以前 2020-12-24 00:27

i\'d like to \"Tail -f\" a lot of logfiles from within a java app.

I\'ve gotten this to work by monitoring the size and last update and repeatedly opening the file a

3条回答
  •  盖世英雄少女心
    2020-12-24 01:28

    Apache Commons has a Tailer class, would that do what you want? If it would, it has rolling detection mechanism as well as the reading stuff, so you'd get all you need.

    If that cannot do it, it might be that there's no way of doing it with pure java. You'd need some help of, for example, C code, utilizing fopen with SH_DENYNO parameter allowing shared open on windows. Or then just call a tail implementation by executing a system command.

    But since the code opening the log file is the one causing the lock on it, even that might not help. In that case the only real option is to change the way your logging works, as that is the culprit locking the file. Log4j can use a SocketAppender, among other things.

提交回复
热议问题