在Java中显示文件中的一些行 [英] Display some lines from file in java

查看:56
本文介绍了在Java中显示文件中的一些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10行的txt文件,我想编写Java代码以显示3到5行.

I have a txt file that contains 10 lines, and I would like to write Java code to display lines 3 through 5.

有人可以帮我吗?

推荐答案

  void Scan(String filename, int start, int end) {
      Scanner in = null;
      try {
          in = new Scanner(new File(filename));
      } catch(FileNotFoundException e) {
          e.printStackTrace();
      }
      int line = 1;
      while(line < start) {
          in.nextLine();
          line++;
      }
      while(line <= end) {
          System.out.println(in.nextLine());
          line++;
      }
  }

}

这篇关于在Java中显示文件中的一些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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