如何在android上更快地将文本文件读取到ArrayList [英] how to faster read text file to ArrayList on android
问题描述
我在将 txt 文件快速读取到 ArrayList 时遇到问题.如果我想读取 0.9MB 的文件,我必须等待 5 分钟.如果文件大小为 34MB(部分原因是因为 android 不接受大于 1MB 的文件),则它完全不起作用.我认为这个过程最多应该是几秒钟.
I have a problem with fast read txt file to ArrayList. If I want read a file size 0,9MB, I must wait 5min. If files size is 34MB (partialy, because android does not accept larger than 1MB files), it's completely not working. I think that process should be max few second.
这是一个代码:
String word;
public ArrayList<String> dictionary = new ArrayList<String>();
public void setup()
{
try {
AssetManager assetManager = getAssets();
InputStream inputf;
inputf = assetManager.open("dict_1.txt");
reader = new BufferedReader(new InputStreamReader(inputf));
word = " ";
while(word != null)
{
word = reader.readLine();
if (word != null)
dictionary.add(word);
}
if(reader.equals("null")) println("No file found");
} catch (NullPointerException e) {
e.printStackTrace();
println("No file found");
} catch (IOException e) {
e.printStackTrace();
}
}
我为我的英语感到抱歉.我希望一切都可以理解.
I'm sorry for my english. I hope that all is understadable.
推荐答案
您的 ArrayList
在您添加项目时不断重新分配.这会消耗大量的 CPU 时间,尤其是随着列表的增长,因为需要在内存中复制一堆指针.更好的方法是将条目数存储为字典文件的第一项,然后预先分配ArrayList
:
Your ArrayList
keeps getting reallocated as you're adding items. This can consume a non-trivial amount of CPU time, especially as the list grows, since a bunch of pointers need to copied around in memory. A better approach would be to store the number of entries as the first item of the dictionary file, and then pre-allocate the ArrayList
:
dictionary = new ArrayList<String>(numberOfEntries);
更高级的优化将是一种完全不依赖于 Java 集合类的数据结构.根据您的需要,这可能是一个巨大的 UTF-8 字节数组,它可以一次性读入内存(甚至可以通过内存映射文件访问).
A more advanced optimization would be a data structure that doesn't rely on a Java collection class at all. Depending on your needs, this could be a huge UTF-8 byte array that is read into memory in one swoop (or even accessed via a memory-mapped file).
这篇关于如何在android上更快地将文本文件读取到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!