所以我遇到的问题是,代码是从第1-3行读取文本文件,我希望它从第3-5行读取文本文件。我已经设置好了代码,我需要弄清楚如何从第3行开始,它现在读3行很好,但是由于它不是从3开始,所以它没有读取正确的行并输出它们
这是到目前为止我的代码
public void display(int from, int to) throws FileNotFoundException {
Scanner sc = new Scanner(new File(fileName));
File f = new File(fileName);
if (f.exists()) {
while (sc.hasNextLine() && from <= to){
System.out.println(sc.nextLine());
from++;
}
} else {
System.out.print("This File Does Not Exist");
}
sc.close();
}}
目前它给我的输出是"1-Lorem ipsum dolor同2-Consectetuer adipiscing elit 3-Sed diam the nibh euismod tincidunt“。
这是我文本中的文件,实际上,我需要它从第三行开始,这就是我需要帮助的全部内容。
发布于 2022-11-20 00:59:29
只需创建一个循环,忽略不想读的前两行
for (int i = 0; i < from-1; i++) {
if (sc.hasNextLine()) sc.nextLine();
}https://stackoverflow.com/questions/74504892
复制相似问题