Reputation: 17
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
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