我正在开发一个从文本文件中读取单词的字典应用程序,但文本文件的大小为 10mb,因此由于内存限制,我无法在模拟器或设备上运行它。
那么这个问题的解决方案是什么?我可以在压缩时从 zip 中读取文本文件,还是将其拆分为 10 个单独的文本文件,每个文件 1mb 更好?
以下是当前读取文本文件的代码,我需要对代码进行哪些更改?
private synchronized void loadWords(Resources resources) throws IOException {
if (mLoaded) return;
Log.d("dict", "loading words");
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, ":");
if (strings.length < 2) continue;
addWord(strings[0].trim(), strings[1].trim());
}
} finally {
reader.close();
}
mLoaded = true;
}
public synchronized List<Word> getAllMatches(Resources resources) throws IOException {
List<Word> list = new ArrayList<Word>();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, ":");
if (strings.length < 2) continue;
Word word = new Word(strings[0].trim(), strings[1].trim());
list.add(word);
}
} finally {
reader.close();
}
return list;
}