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();
目前,我一次一行读取完整文件,然后将其附加到字符串中。有没有更快的方法来做到这一点?我正在寻找一种将整个文件放入字符串的快速方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 StringBuilder 构建大型字符串。
既然有String,为什么还要使用StringBuilder?
Use StringBuilder to build large Strings.
Why StringBuilder when there is String?
我发现您似乎已经根据凯文的评论有了一个可行的解决方案,但这可能有助于理解为什么您的旧代码很慢。
问题是,当您直接使用 + 运算符或 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 theconcat()
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.
假设 Apache Commons 在 Android 上运行良好,您可以使用这样的东西吗?
(或者您可以使用 Guava 及其 CharStreams 帮助程序,但它通常更冗长)
除非您有理由想要避免使用第 3 方库?
Assuming Apache Commons works fine on Android, could you use something like this?
(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?