【发布时间】:2012-10-11 11:00:54
【问题描述】:
我有一个包含超过 200k 行的大型 txt 文件。每行1个字。 我需要从这个文件中加载所有单词,它在我的 Nexus 7 上工作正常,但是当我尝试在模拟器上启动我的应用程序时,我得到了 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 设备的内存通常(非常)有限。
-
添加堆栈跟踪会有所帮助。