Java Android reading full file quickly
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 + 开发者_JS百科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.
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.
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.
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?
精彩评论