我正在写小游戏,我需要读取文本文件,将其写入数组,然后打印数组。我的读取和返回数组的类如下所示:
import java.io.*;
import java.util.*;
public class WordsList {
public String[] wordsList;
public void readFile() throws Exception{
FileInputStream in = new FileInputStream("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
NumberOfLines read = new NumberOfLines();
int n = read.getLineCount();
String strLine;
wordsList = new String[n];
for (int j = 0; j < wordsList.length; j++){
wordsList[j] = br.readLine();
}
in.close();
}
public String[] returnsWordList(){
return wordsList;
}
}
在主要课程中,我有这个:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
WordsList words = new WordsList();
System.out.println(words.returnsWordList());
}
}
它返回空值而不是单词......我做错了什么?有任何想法吗?