Java Android 快速读取完整文件

发布于 2024-12-04 13:05:41 字数 746 浏览 0 评论 0原文

我有以下代码片段将文本文件的完整内容读取到字符串中。它可以工作,唯一的问题是......它真的很慢(文件大约有 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();

目前,我一次一行读取完整文件,然后将其附加到字符串中。有没有更快的方法来做到这一点?我正在寻找一种将整个文件放入字符串的快速方法。

I have the following code snippet to read the full contents of a text file into a string. It works, only problem is... it's really slow (the file is about 1500 lines long).

            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();

At the moment, I'm reading the full file in one line at a time, then appending it to the string. Is there a faster way to do this? I'm looking for a quick way to get the entire file into a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-12-11 13:05:41

使用 StringBuilder 构建大型字符串。

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,为什么还要使用StringBuilder?

字符串不允许追加。对 String 调用的每个方法都会创建一个新对象并返回它。这是因为 String 是不可变的 - 它无法更改其内部状态。

Use StringBuilder to build large Strings.

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();

Why StringBuilder when there is String?

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.

晌融 2024-12-11 13:05:41

我发现您似乎已经根据凯文的评论有了一个可行的解决方案,但这可能有助于理解为什么您的旧代码很慢。

问题是,当您直接使用 + 运算符或 concat() 方法连接字符串时,它实际上是使用连接结果创建一个新字符串。这涉及复制要连接的两个字符串的全部内容。因此,当您读取文件并构建内容字符串时,每次添加内容时,您都会创建迄今为止内容中所有内容的另一个副本。

我认为您现在正在做的解决方案是使用 StringBuffer 类,它是一个动态缓冲区,允许您修改其内容(即在末尾附加新数据),无需 总是必须分配一个新的 String 对象并将所有内容复制到其中。

您必须记住的主要事情是 java 中的字符串是不可变的。任何时候你在 java String 上调用一个以某种方式更改它的方法时,它实际上是在创建带有修改结果的字符串的新副本。

I see that you already seem to have a working solution based on Kevin's comment, but it might be helpful to understand why your old code was slow.

The problem is that when you concatenate strings directly with the + operator or with the concat() method, it is actually creating a new string with the result of the concatenation. This involves copying the entire contents of both strings that you are concatenating. So as you read in the file and build up the contents string, every time you add to it, you are creating yet another copy of everything that you have so far in contents.

The solution, which I assume is what you're doing now, is to use a StringBuffer class, which is a dynamic buffer that allows you to modify its contents (i.e. append new data to the end), without always having to allocate a new String object and copy everything to it.

The main thing that you have to keep in mind is that Strings in java are immutable. Any time you call a method on a java String that changes it in some way, it is actually creating a new copy of the string with the result of the modification.

铁轨上的流浪者 2024-12-11 13:05:41

假设 Apache Commons 在 Android 上运行良好,您可以使用这样的东西吗?

InputStream is = this.getResources().openRawResource(R.raw.comp_rules_glossary);
String text = IOUtils.toString(is, "UTF-8");

(或者您可以使用 Guava 及其 CharStreams 帮助程序,但它通常更冗长)

除非您有理由想要避免使用第 3 方库?

Assuming Apache Commons works fine on Android, could you use something like this?

InputStream is = this.getResources().openRawResource(R.raw.comp_rules_glossary);
String text = IOUtils.toString(is, "UTF-8");

(or you could use Guava and its CharStreams helper, but it's usually more verbose)

Unless there's a reason you want to avoid 3rd party libs?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文