从流的末尾开始的StreamReader.ReadLine() [英] StreamReader.ReadLine() starting from the end of the stream

查看:490
本文介绍了从流的末尾开始的StreamReader.ReadLine()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#/.NET中工作,并且正在解析文件以检查一行是否与特定正则表达式匹配.实际上,我想找到匹配的 last 行.

I'm working in C#/.NET and I'm parsing a file to check if one line matches a particular regex. Actually, I want to find the last line that matches.

要获取文件的行,我目前正在使用System.IO.StreamReader.ReadLine()方法,但是由于我的文件非常大,因此我想对代码进行一些优化并从末尾开始文件.

To get the lines of my file, I'm currently using the System.IO.StreamReader.ReadLine() method but as my files are very huge, I would like to optimize a bit the code and start from the end of the file.

有人知道从流的末尾开始,C#/.NET中是否存在与ReadLine()类似的功能?如果不是这样,那么在您看来,做上述工作的最简单,最优化的方法是什么?

Does anyone know if there is in C#/.NET a similar function to ReadLine() starting from the end of the stream? And if not, what would be, to your mind, the easiest and most optimized way to do the job described above?

推荐答案

有趣的是您应该提到它-是的,我有.我前一段时间写了ReverseLineReader,并将其放在 MiscUtil 中.

Funny you should mention it - yes I have. I wrote a ReverseLineReader a while ago, and put it in MiscUtil.

这是对的回答关于堆栈溢出-答案包含了代码,尽管它也使用了MiscUtil的其他位.

It was in answer to this question on Stack Overflow - the answer contains the code, although it uses other bits of MiscUtil too.

它只会处理某些编码,但希望能满足您的所有编码要求.请注意,如果您不得不读取整个文件,这将比从文件的开头读取效率低-各种事物都可能在文件中呈向前运动,因此为此进行了优化.但是,如果您实际上只是在读取文件末尾附近的行,那将是一个巨大的胜利:)

It will only cope with some encodings, but hopefully all the ones you need. Note that this will be less efficient than reading from the start of the file, if you ever have to read the whole file - all kinds of things may assume a forward motion through the file, so they're optimised for that. But if you're actually just reading lines near the end of the file, this could be a big win :)

(不确定是否应该进行近距离投票...)

(Not sure whether this should have just been a close vote or not...)

这篇关于从流的末尾开始的StreamReader.ReadLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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