1
我在寫小遊戲,我需要讀取文本文件,將其寫入數組,然後打印數組。我的閱讀和返回數組類看起來是這樣的:將文本文件寫入數組,然後使用Java返回
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());
}
}
它返回空值,而不是的話... 我做了什麼錯?有任何想法嗎?
謝謝,我改變了代碼太多次了,迷失了自我,我把它做好了,但我忘記了toString。 (Java noob在這裏)謝謝! – Shepard 2012-08-05 12:26:35