Android快速阅读依赖的代码,Java Android快速阅读完整文件
我有以下代碼片段將文本文件的全部內容讀入字符串.它有效,唯一的問題是……它真的很慢(文件長約1500行).
InputStream is = this.getResources().openRawResource(R.raw.comp_rules_glossary);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String previousLine = "start";
while ((readLine = br.readLine()) != null)
{
rules = rules + readLine + eol;
if (previousLine.equals(""))
{
content = content + readLine + eol;
}
previousLine = readLine;
}
is.close();
br.close();
目前,我正在一次讀取一行中的完整文件,然后將其附加到字符串中.有更快的方法嗎?我正在尋找一種快速的方法將整個文件放入字符串中.
解決方法:
StringBuilder sb_rules = new StringBuilder();
StringBuilder sb_content = new StringBuilder();
while ((readLine = br.readLine()) != null)
{
sb_rules.append(readLine);
sb_rules.append(eol);
if (previousLine.equals(""))
{
sb_content.append(readLine);
sb_content.append(eol);
}
previousLine = readLine;
}
content = sb_rules.toString();
content = sb_content.toString();
String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable – it cannot change its internal state.
標簽:java,file,android,bufferedreader,performance
來源: https://codeday.me/bug/20190621/1253193.html
總結
以上是生活随笔為你收集整理的Android快速阅读依赖的代码,Java Android快速阅读完整文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 检测当前wifi是否又网
- 下一篇: android 双击home退出程序,A