> Hi, I'm trying to read multiple text lines from a file into a single
> String. The approach I am using is to read each line of the file
[quoted text clipped - 6 lines]
> result = result + buffer;
> }
first of all you should be using StringBuffer.append() instead of String
concatenation, its many many times faster.
Secondly, there is nothing stopping you from using the method
BufferedReader.read(char []...)
with a char array of, say, 1-8 KB, which you add to the StringBuffer
repeatedly. Just make sure you set the size of the BufferedReader to at
least the array size.
/tom
Jeffrey Schwab - 22 Feb 2006 22:33 GMT
>> BufferedReader br = new BufferedReader(fr);
>> String buffer, result;
>> while ((buffer = br.readLine()) != null) {
>> result = result + buffer;
>> }
...
> Secondly, there is nothing stopping you from using the method
> BufferedReader.read(char []...)
> with a char array of, say, 1-8 KB, which you add to the StringBuffer
> repeatedly. Just make sure you set the size of the BufferedReader to at
> least the array size.
That's a much nicer solution. It is a little different from the
original though, in that it preserves line terminators.
Digital Puer - 23 Feb 2006 00:02 GMT
Thank you for the clear explanation.
> > Hi, I'm trying to read multiple text lines from a file into a single
> > String. The approach I am using is to read each line of the file
[quoted text clipped - 17 lines]
>
> /tom
> Hi, I'm trying to read multiple text lines from a file into a single
> String. The approach I am using is to read each line of the file
[quoted text clipped - 5 lines]
> while ((buffer = br.readLine()) != null) {
> result = result + buffer;
On the first pass, "result" has not been initialized.
> }
>
> Is there a better, faster way?
Use a StringBuilder or a StringBuffer. The StringBuilder theoretically
should be a little faster, because it does not provide any guarantee of
synchronization. On my system, they are equally fast. In the program
below, each takes ~300ms to process the input file, plus a 50-ms hit to
whichever is tested first. Here's the output of a run on my system:
> java -cp . Main
Using a StringBuilder took 350 ms.
Using a StringBuffer took 301 ms.
^C
OK, so I killed the String test. It was slowing down my machine.
Here's the code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
class Main {
static String readWithString(Reader fr)
throws IOException {
BufferedReader br = new BufferedReader(fr);
String buffer, result = "";
while ((buffer = br.readLine()) != null) {
result = result + buffer;
}
return result;
}
static String readWithStringBuffer(Reader fr)
throws IOException {
BufferedReader br = new BufferedReader(fr);
String line;
StringBuffer result = new StringBuffer();
while ((line = br.readLine()) != null) {
result.append(line);
}
return result.toString();
}
static String readWithStringBuilder(Reader fr)
throws IOException {
BufferedReader br = new BufferedReader(fr);
String line;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
result.append(line);
}
return result.toString();
}
public static void main(String[] args)
throws IOException {
/* Using each of several buffer types, record the time taken to
* concatenate all lines from a text file, without newlines.
*
* The sample text file is ~ 3MB, courtesy of Project Gutenberg.
* http://www.gutenberg.org/
*/
FileReader f = new FileReader("war_and_peace.txt"); // ~3MB
try {
long t = new Date().getTime();
readWithStringBuilder(f);
t = new Date().getTime() - t;
System.out.println(
"Using a StringBuilder took " + t + " ms.");
} finally {
f.close();
}
f = new FileReader("war_and_peace.txt"); // ~3MB.
try {
long t = new Date().getTime();
readWithStringBuffer(f);
t = new Date().getTime() - t;
System.out.println(
"Using a StringBuffer took " + t + " ms.");
} finally {
f.close();
}
f = new FileReader("war_and_peace.txt"); // ~3MB.
try {
long t = new Date().getTime();
readWithString(f);
t = new Date().getTime() - t;
System.out.println(
"Using a String took " + t + " ms.");
} finally {
f.close();
}
}
}
Digital Puer - 22 Feb 2006 23:04 GMT
> OK, so I killed the String test. It was slowing down my machine.
LOL. Point well understood. Thank you very much for the code.