如何将文件中的项目输入到String数组中?这是我到目前为止的内容,但结果一直显示为空!有人知道可能是什么问题吗?
public static void main(String[] args) {
File inFile = new File("GUGU.txt"); // connect the program and file
String input;
int numLines = 0;
String[] words = new String[50];//large enough to store 50 names
int big = 0;
String firstLine = "";
if (inFile.exists() && inFile.length() != 0) {
try {
System.out.println("File read");
BufferedReader in = new BufferedReader(new FileReader(inFile));
input = in.readLine();
while (input != null) { // continue while not end of file
if (input.length() > big) {
big = input.length();
}
words[numLines] = input;
numLines++;
input = in.readLine();
System.out.println(words[numLines]);
}
} catch (java.io.IOException e) {
System.out.println(e);
}
}
}
将移至println
,然后再递增numlines
变量。您正在打印数组中的下一个内容,因为该数组为空,所以它为null。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句