我有大的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
添加堆栈跟踪会有所帮助。 –