Tail -n 1000 in Java (Apache commons, etc)

2019-04-09 13:19发布

I'm wondering if util code already exists to implement some/all of *NIX tail. I'd like to copy the last n lines of some file/reader to another file/reader, etc.

标签: java tail
4条回答
对你真心纯属浪费
2楼-- · 2019-04-09 14:12

This is a UI app - you can look at the source though to see what it does (basically some threading & IO). Follow.

查看更多
Emotional °昔
3楼-- · 2019-04-09 14:13

This seems like a good bet: Tailer Library. This implementation is based on it, but isn't the same. Neither implement a lookback to get the last 100 lines though. :(

查看更多
仙女界的扛把子
4楼-- · 2019-04-09 14:18

The "last n lines" is quite tricky to do with potentially variable width encodings etc.

I wrote a reverse line iterator in C# in response to another SO question. The code is all there, although it uses iterator blocks which aren't available in C# - you'd probably be better off passing the desired size into the method and getting it to build a list. (You can then convert the yield return statements in my code into list.add() calls.) You'll need to use a Java Charset instead of Encoding of course, and their APIs are slightly different too. Finally, you'll need to reverse the list when you're done.

This is all assuming you don't want to just read the whole file. If you don't mind doing that, you could use a circular buffer to keep "the last n lines at the moment", reading through until the end and returning the buffer afterwards. That would be much much simpler to implement, but will be much less efficient for very long files. It's easy to make that cope with any reader though, instead of just a few selected charsets over a stream (which my reverse iterator does).

查看更多
聊天终结者
5楼-- · 2019-04-09 14:24

You could take a look at this tail implementation in one of Heritrix's utility classes. I didn't write it but I wrote the code that uses it, works correctly as far as I can tell.

查看更多
登录 后发表回答