让我先确立一个事实:我是java的新手,所以请耐心等待。Java扫描仪从文件中读取
在我的编程课上,我们接受了一个在家里使用Scanner类的练习。活动说明如下编码行使有:
import java.io.*;
import java.util.*;
public class FileReadWrite {
public static void main(String[] args) throws FileNotFoundException {
String[] strArr = new String[100];
int size = 0;
try {
Scanner scFile = new Scanner(new File("Names1.txt"));
while (scFile.hasNext()) {
strArr[size] = scFile.next();
size++;
}
scFile.close();
for (int i = 0; i < size; i++) {
System.out.print(strArr[i] + " ");
}
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
}
}
程序似乎不能工作正确。我使用NetBeans来运行代码,当我运行代码时,它不会在文本文件Names.txt中显示数据。这是为什么?然而,该程序确实完全没有错误。
我尝试过扫描仪类javadocs,但它没有帮助我。
请向我解释,以便我可以从所犯的错误中学习。
感谢, 约翰
是否有错误,或者......? –
请详细说明“似乎不工作”。 –
@kbbucks新的String()如何提供帮助? –