需要相应地跳过读取文件时的行号(阅读更多)

the line number while reading file needs to be skipped accordingly(read more)

我想用多行读取多个文件,一个一个地读取文件。从每个文件中,我希望每 3 行都存储在一个字符串数组中(即行号 1、4、7、10,.... 等等必须存储在字符串数组中)我尝试了这段代码,但它仍然将所有三个文件的每一行都保存在数组中。但我想跳过两行并在数组中每隔三行添加一次。 帮帮我。

这是代码

import java.io.*;
import java.util.*;

public class music {
public double xx[] = new double[5];
public String songs[] = new String[40000];
//public static File f = new File("C:/Users/Prashant/Desktop/OCEAN/f.txt");
public static File[] fileList1 = new File("C:/Users/Prashant/Desktop/MUSIC/Categories").listFiles();

public void selectsongs() throws IOException, FileNotFoundException, ArrayIndexOutOfBoundsException //, NoSuchElementException
{
    int i;
    int lineno = 1;
    int songno = 0;
    String t = "";
    xx[0]=51;
    xx[1]=10;
    xx[2]=60;
    xx[3]=60;
    xx[4]=60;

    if (xx[0] >= 50 && xx[1] < 50 && xx[2] >= 50 && xx[3] >= 50 && xx[4] >= 50) {
        for (i = 0; i < 3; i++) {
            Scanner scanner1 = new Scanner(fileList1[i]);
            lineno = 1;
            while (scanner1.hasNextLine()) {
                t = "";
                if (lineno % 3 == 1) {
                    t = t + scanner1.nextLine();
                    songs[songno] = t;
                    songno++;
                }
                lineno++;
            }
        }
    }
    for(int j=0;j<songno;j++)
    {
        System.out.println(songs[j]);
    }
}
public static void main(String args[])
{

}
}

如果调用nextLineScanner只会前进一行。 hasNextLine不影响状态。因此,您必须在 while 循环的每次迭代中调用 nextLine,但只在适当的时候将 return 值放入数组中。