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

后端 未结 9 1686
长发绾君心
长发绾君心 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:20

    If all it really does have to do is print last 5 lines:

            ArrayList lines = new ArrayList();
    
            String tmp="";
            while ((tmp = br.readLine()) != null) {
                lines.add(tmp);
            }
            for (int i = lines.size()-5; i < lines.size(); i++) {
                System.out.println(lines.get(i-1));
            }
    

提交回复
热议问题