miman
miman

Reputation: 17

Display some lines from file in java

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

Could anyone help me?

Upvotes: 0

Views: 274

Answers (1)

Neil Masson
Neil Masson

Reputation: 2689

  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++;
      }
  }

}

Upvotes: 2

Related Questions