在 native C# 中,如何从文件末尾开始读取?
这是中肯的,因为我需要读取一个日志文件,读取 10k,读取最后 3 行是没有意义的。
最佳答案
读取最后 1024 个字节:
using (var reader = new StreamReader("foo.txt"))
{
if (reader.BaseStream.Length > 1024)
{
reader.BaseStream.Seek(-1024, SeekOrigin.End);
}
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
关于c# - 从末尾开始读取文件,类似于 tail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4368857/