By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

opening text files

P: n/a
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?

Thanx,
Tony G.

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"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
Jul 17 '05 #2

P: n/a
That looks great!

I'll give it a try tomorrow first thing.

thank you,
Tony G.
Anthony Borla wrote:
"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


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.