以下是函数的代码:
public void loadTextFile(String textFileName, ArrayList<String> dictionary)
{
AssetManager assetManager = getAssets(); //files manager
//reader = public bufferedReader
//word = public String
//dictionary = public ArrayList<String>
//load file to buffer
try {
reader = new BufferedReader(new InputStreamReader(assetManager.open(textFileName)));
} catch (IOException e1) {
e1.printStackTrace();
}
word = " ";
//loop, save words from buffer to dictionary
while(word != null)
{
try {
word = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
word = null;
}
if (word != null) {
dictionary.add(word);
}
}//end while
//buffer close
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} //end function
如果我尝试加载小于1MB的小文件,它工作得很好,而且相当快。如果文件(部分)大于1MB,但小于3MB,则运行缓慢(4-6分钟)。如果文件较大,则无法工作,并且我在仿真器中有以下信息:“应用程序应用程序(进程processing.test.app)已意外停止。请重试”。我不知道哪里出了问题。我尝试过:预先分配ArrayList,加载文件到几个数组列表,但仍然不起作用。如果有任何建议,我将不胜感激。
发布于 2014-02-16 07:47:44
首先,word可能在文件中的某处变为null,循环将退出(因为您在出现异常时显式地将word设置为null )。你得到堆栈跟踪了吗?
如果您有一个AsyncTask
将代码移动到后台进程
https://stackoverflow.com/questions/21805217
复制相似问题