循环通过FileInputStream时的无限循环

Wal*_* IV 0 java infinite-loop fileinputstream

好吧,所以我正在编写一个Java应用程序来导入一个csv文件,然后遍历结果,并将它们加载到一个数组中.我正在导入文件,因为它没有通过异常.我的问题是,当我尝试计算FileInputStream中的记录数时,我陷入了无限循环.这可能是什么问题.下面是代码:

这是我的类,有一个调用go()的Main方法:

public void go() {
    pop = new PopularNames();
    popGui = new PopularNamesGui();
    String file = popGui.userInput("Enter the correct name of a file:");
    pop.setInputStream(file);
    pop.getNumberOfNames();
}
Run Code Online (Sandbox Code Playgroud)

这是类PopularNames(pop),在下面的方法中,我将inputStream var设置为新的FileINputStream.文件名由用户提供.

public void setInputStream(String aInputStream) {
    try {
        inputStream = new Scanner(new FileInputStream(aInputStream));
    } catch (FileNotFoundException e) {
        System.out.println("The file was not found.");
        System.exit(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是麻烦的方法.我只是循环遍历FileInputStream并计算记录数:

public void getNumberOfNames() {
    while (this.inputStream.hasNext()) {
        fileDataRows++;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pri*_*ley 6

public void getNumberOfNames() {   
  while (this.inputStream.hasNext()) {   
     inputStream.nextLine(); // Need to read it so that we can go to next line if any
     fileDataRows++;    
  } 
}
Run Code Online (Sandbox Code Playgroud)