简体   繁体   English

在java中读取文本文件时读取特定行

[英]Reading particular line while reading text file in java

My question is i come up with a situation that i have a text file that contains following data including 15. 我的问题是我提出了一个情况,我有一个文本文件,其中包含以下数据,包括15。

15 // first line 15 //第一行

produce,3554,broccoli,5.99,1 //second line 生产,3554,西兰花,5.99,1 //第二行

i am using the following code to read the text in the file and printing its output. 我使用以下代码来读取文件中的文本并打印其输出。

try

{

File f = new File("filename.txt");


Scanner s= new Scanner(f);

s.hasNext();

String no = s.nextLine();
int num = Integer.parseInt(no);

System.out.println(num);// which prints number 15 only

}

now i want to read second line leaving first one which is "produce,3554,broccoli,5.99,1" in another variable and print it as i print 15 without using a loop . 现在我想读第二行,在另一个变量中留下第一行“产品,3554,西兰花,5.99,1”并打印出来,因为我打印15而不使用循环。 Is there any way that i can read second line. 有什么办法可以让我读到第二行。

String secondLine = s.nextLine();
System.out.println(secondLine);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM