"tonyg" <to***@rollanet.org> wrote in message
news:TC********************@twister01.bloor.is.net .cable.rogers.com...
Hi everyone,
I was wondering if there is an efficient or fast or preferred
way to open text files in java. I want to load the text file
into a JTextPane.
The way I do it now is...
.....
BufferedReader objBrIn = new BufferedReader(
new FileReader(this.file));
String strTemp;
String strOut = "";
try {
while ((strTemp = objBrIn.readLine()) != null) {
strOut += (strTemp + newline);
}
objTextPane.setText(strOut);
.....
but that takes forever to open and display even moderately
large text files.
Does anyone know a better way to do this?
I don't think your problem is due to 'slow' stream reading as much as it is
to do with [needlessly] creating too many 'String' objects. For example,
this:
strOut += (strTemp + newline);
is grossly inefficient; far better to do this:
StringBuffer strOut;
...
strOut.append(strTemp).append(newline);
...
objTextPane.setText(strOut.toString());
If this doesn't improve performance [though I beleieve it] try reading the
file - using a single 'read' call - into an array, then wrap this inside a
'StringBuffer', and return a 'String' via:
...
objTextPane.setText(strOut.toString());
as shown above.
I hope this helps.
Anthony Borla