快速阅读文本文件的最后一行? [英] Quickly read the last line of a text file?

查看:111
本文介绍了快速阅读文本文件的最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

从Java中的[非常大的]文件中读取最后一行文本是最快最有效的方法>

看看我对类似C#的问题。代码将非常相似,尽管编码支持在Java中有所不同。



基本上,这不是一件非常简单的事情。正如MSalter指出的那样,UTF-8确实可以轻松地将 \r \\\
作为UTF这些字符的-8表示与ASCII相同,这些字节不会发生在多字节字符中。



所以基本上,要缓冲(说)2K,并逐渐向后读(跳到2K之前,之前,读下一个2K)检查线终止。然后跳到正确的流中的正确位置,在顶部创建一个 InputStreamReader ,另外还有一个 BufferedReader 那。然后调用 BufferedReader.readLine()


What's the quickest and most efficient way of reading the last line of text from a [very, very large] file in Java?

解决方案

Have a look at my answer to a similar question for C#. The code would be quite similar, although the encoding support is somewhat different in Java.

Basically it's not a terribly easy thing to do in general. As MSalter points out, UTF-8 does make it easy to spot \r or \n as the UTF-8 representation of those characters is just the same as ASCII, and those bytes won't occur in multi-byte character.

So basically, take a buffer of (say) 2K, and progressively read backwards (skip to 2K before you were before, read the next 2K) checking for a line termination. Then skip to exactly the right place in the stream, create an InputStreamReader on the top, and a BufferedReader on top of that. Then just call BufferedReader.readLine().

这篇关于快速阅读文本文件的最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆