【发布时间】:2016-04-21 17:55:36
【问题描述】:
我想读取一个文本文件并将每一行放入一个字符串(字符串数组)中。然而,这需要扫描文件两次,一次是为了弄清楚有多少行,另一次是创建一个该大小的字符串数组。但它会引发错误,并且重置方法似乎不起作用。
FileReader read = null;
try {
read = new FileReader("ModulesIn.txt");
//scan through it and make array of strings - for each line
Scanner scan = new Scanner(read);
while(scan.hasNextLine()){
numOfMods++;
scan.nextLine();
}
scan.reset();
lines = new String[numOfMods];
for(int i = 0; i < numOfMods; i++)
lines[i] = scan.nextLine();
这是相关代码的 sn-p。
【问题讨论】:
标签: java java.util.scanner nosuchelementexception