How to efficiently read only last line of the text file

前端 未结 3 404
盖世英雄少女心
盖世英雄少女心 2021-01-18 18:48

Need to get just last line from big log file. What is the best way to do that?

3条回答
  •  梦谈多话
    2021-01-18 19:25

    You want to read the file backwards using ReverseLineReader:

    How to read a text file reversely with iterator in C#

    Then run .Take(1) on it.

    var lines = new ReverseLineReader(filename);
    var last = lines.Take(1);
    

    You'll want to use Jon Skeet's library MiscUtil directly rather than copying/pasting the code.

提交回复
热议问题