Java Scanner找不到下一行?

Cha*_*Kay 2 java line

出于某种原因,当我尝试扫描.txt文件时,它无法找到任何行,从而导致错误:

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)  
Run Code Online (Sandbox Code Playgroud)

码:

File file = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
Scanner scanner = new Scanner(new FileInputStream (file), "UTF-8");

    while(scanner.hasNextLine()){
         modsList.add(scanner.nextLine());       
         System.out.println(scanner.nextLine());
     }
Run Code Online (Sandbox Code Playgroud)

我没有关闭扫描仪.modsList是一个List,我尝试添加每一行,以便我可以检查聊天(用户)中的mod是否与列表中的一个匹配,但错误是在System.out上...

我检查了.txt文件是否存在以下内容:

 File f = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");

            if(f.exists() && !f.isDirectory()) { 
                System.out.println("file exists");
            }
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么线条没有被读取?

.txt内容:

abkayckay
kayc01
Run Code Online (Sandbox Code Playgroud)

谢谢,任何帮助表示赞赏.

Ell*_*sch 5

您当前正在读取两行而不是一行,保存您读取的行以添加到列表中使用相同的行显示.

while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    modsList.add(line);       
    System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud)