我想循环遍历HTML文档的各行并打印输出。
通常我会做这样的事情
URL url = new URL("http://google.com");
Scanner scanner = new Scanner(url.openStream());
while(scanner.hasNext())
System.out.println(scanner.nextLine());
但是,假设我想从第30行开始打印内容,而不是从第1行开始打印,那么我该怎么办?
发布于 2019-02-02 10:34:57
您可以先调用scanner.nextLine
30次,然后开始打印。例如:
// go through the first 30 lines without printing them...
for (int i = 0 ; i < 30 && scanner.hasNextLine() ; i++) {
scanner.nextLine();
}
// and now print the remaining lines
while(scanner.hasNextLine())
System.out.println(scanner.nextLine());
https://stackoverflow.com/questions/54492110
复制