Java 中的 Tail -n 1000 (Apache commons 等)
我想知道 util 代码是否已经存在来实现部分/全部 *NIX tail。 我想将某个文件/阅读器的最后 n 行复制到另一个文件/阅读器等。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎是一个不错的选择:Tailer Library。 此实现基于它,但并不相同。 不过,两者都没有实现回溯来获取最后 100 行。 :(
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. :(
您可以看一下尾部实现< /a> 在 Heritrix 的实用程序类之一中。 我没有编写它,但我编写了使用它的代码,据我所知,它工作正常。
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.
这是一个 UI 应用程序 - 您可以查看源代码以了解它的作用(基本上是一些线程和 IO)。 关注。
This is a UI app - you can look at the source though to see what it does (basically some threading & IO). Follow.
“最后 n 行”对于潜在的可变宽度编码等来说非常棘手。
我用 C# 编写了一个反向行迭代器来响应 另一个 SO 问题。 代码就在那里,尽管它使用了 C# 中不可用的迭代器块 - 您最好将所需的大小传递到方法中并让它构建一个列表。 (然后,您可以将代码中的
yield return
语句转换为list.add()
调用。)您需要使用 JavaCharset
> 当然不是Encoding
,它们的 API 也略有不同。 最后,完成后您需要反转该列表。这一切都假设您不想只想读取整个文件。 如果您不介意这样做,您可以使用循环缓冲区来保留“当前的最后 n 行”,读取直到结束并随后返回缓冲区。 这实现起来会简单得多,但对于很长的文件来说效率会低得多。 不过,很容易处理任何阅读器,而不仅仅是流上的几个选定的字符集(我的反向迭代器就是这样做的)。
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 intolist.add()
calls.) You'll need to use a JavaCharset
instead ofEncoding
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).