Java 如何跳过文件的第一行?

Java 如何跳过文件的第一行?,java,file,Java,File,我从文件中读取并存储在数组中,文件的第一行只包含1,第二行包含一个从空格键拆分的字典单词。那么我如何从第二行读取文件呢 try { File text = new File ("dictionary.txt"); Scanner file = new Scanner(new File("dictionary.txt")); while(file.hasNextLine()) { Syst

我从文件中读取并存储在数组中,文件的第一行只包含1,第二行包含一个从空格键拆分的字典单词。那么我如何从第二行读取文件呢

try
        {
         File text = new File ("dictionary.txt");
         Scanner file = new Scanner(new File("dictionary.txt"));

         while(file.hasNextLine())
         {
          System.out.println("Level 1");
           int level1 = file.nextInt();
           file.nextLine();



           for(int i = 1; i < 7; i++)
            {
             String [] array = content.split(" ");

             String A = array[0];
             String B = array[1];
             String C = array[2];

            System.out.println(B);
           }
         }
         file.close();
        }
该文件的格式为

一,

蚂蚁是包谁和车哭做动物园狗耳朵 我妈妈吃的都是眼睛发胖的爸爸赢的开心去拿吧用柜台就行了

int count = 0;
while(file.hasNextLine())
{
    count++;
    if (count <= 1) {
      file.nextLine ();
      continue;
    }
    ....
}
我实际上会使用文本而不是重新定义文件来构建扫描仪。宁愿尝试使用资源,也不要显式关闭扫描仪。实际上分配内容,不要为数组迭代硬编码魔术值。基本上,类似于

File text = new File("dictionary.txt");
try (Scanner file = new Scanner(text)) {
    if (file.hasNextLine()) {
        file.nextLine(); // skip first line.
    }
    while (file.hasNextLine()) {
        String content = file.nextLine();
        if (content.isEmpty()) {
            continue; // skip empty lines
        }
        String[] array = content.split("\\s+");
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

您可以添加一个计数器变量来计算您所在的行号,以及当计数器==1时(假设您从1开始),使用continue转到循环的下一个迭代。如果您认为在其他任何时候使用行号都没有用,您可以使用布尔值。呃,读一下?在循环之前?
File text = new File("dictionary.txt");
try {
    Files.lines(text.toPath()).skip(1).forEach(content -> {
        if (!content.isEmpty()) {
            System.out.println(Arrays.toString(content.split("\\s+")));
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}