How to read last 5 lines of a .txt file into java

后端 未结 9 1640
长发绾君心
长发绾君心 2020-12-15 03:03

I have a text file that consists of several entries such as:

hello
there
my
name
is
JoeBloggs

How would I read the last five entries in des

9条回答
  •  春和景丽
    2020-12-15 03:27

    One very easy way would be to use the CircularFifoBuffer class from the Apache Commons Collections library. It's basically a list of a fixed size that discards old elements when it's full and you add new ones. So you'd create a CircularFifoBuffer of size 5, then add all the lines to it. At the end, it'd contain just the last five lines of the file.

提交回复
热议问题