日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android快速阅读依赖的代码,Java Android快速阅读完整文件

發布時間:2025/4/16 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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快速阅读完整文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。