How to tail a file using a NIO selector, in other words, as lines are added to the file a channel is selected so you can read the lines?

笑着哭i 提交于 2019-12-12 03:21:55

问题


Because you cannot redirect GC logs I am left with the option to redirect it to a file with -Xloggc and then get the contents of this file inside my selector through a file channel of some kind. Basically as lines are being added to my file, the selector is being triggered to read them. That way I can get the GC logs programmatically. Is it possible to do that using NIO?


回答1:


Given GC logs are buffered, I wouldn't worry about a bit of latency. You can just poll the file length periodically and read the data added each time. You can do this in IO, NIO or NIO2.




回答2:


No. FileChannel does not extend SelectableChannel so you can't select with it, and even in C where you can, select() doesn't deliver readable events when the file is extended (it delivers them every time you select, as the file is always readable).



来源:https://stackoverflow.com/questions/9856128/how-to-tail-a-file-using-a-nio-selector-in-other-words-as-lines-are-added-to-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!