[英]Infinite Loop when looping through FileInputStream
Alright, so I am writing a Java application to import a csv file and then loop through the results, and load them into an array. 好吧,所以我正在编写一个Java应用程序以导入一个csv文件,然后遍历结果并将它们加载到数组中。 I am importing the file correctly because it doesn't through an Exception.
我正确导入文件,因为它没有通过异常。 My issues is that when I try to count the number of records in the FileInputStream I am trapped in an infinite loop.
我的问题是,当我尝试计算FileInputStream中的记录数时,我陷入了无限循环。 What could be the issue here.
这里可能是什么问题。 Heres the code:
这是代码:
This is my class with a Main method which calls go(): 这是我的带有Main方法的类,该方法调用go():
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();
}
This is the class PopularNames (pop), and in the below method I am setting the inputStream var to a new FileINputStream. 这是类PopularNames(pop),在以下方法中,我将inputStream var设置为新的FileINputStream。 The file name is provided by the user.
文件名由用户提供。
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);
}
}
This is the trouble method. 这是麻烦的方法。 Where I am simply looping through the FileInputStream and counting the number of records:
在这里,我只是循环遍历FileInputStream并计算记录数:
public void getNumberOfNames() {
while (this.inputStream.hasNext()) {
fileDataRows++;
}
}
public void getNumberOfNames() {
while (this.inputStream.hasNext()) {
inputStream.nextLine(); // Need to read it so that we can go to next line if any
fileDataRows++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.