我有大的txt文件,其中包含超過200k行。每行1個字。 我需要加載此文件中的所有單詞,它在我的Nexus 7上正常工作,但是當我嘗試在模擬器上啓動我的應用時,出現OutOfMemoryError。問題出在哪裏?我想確保手機用戶不會有這樣的錯誤。讀取大文本文件時模擬器上出現OutOfMemoryError
public static ArrayList<String> getWords(Context context) throws IOException {
ArrayList<String> words = new ArrayList<String>(300000);
InputStream is = context.getAssets().open("words.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String word;
while ((word = br.readLine()) != null) {
words.add(word.toUpperCase());
}
is.close();
br.close();
return words;
}
您是否考慮過使用數據庫? Android設備通常具有(非常)有限的RAM。 – phineas
添加堆棧跟蹤會有所幫助。 –