468,249 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

opening text files

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
2 8791

"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
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.

Similar topics

16 posts views Thread by iwdu15 | last post: by
11 posts views Thread by moony marouane | last post: by
2 posts views Thread by OutdoorGuy | last post: by
8 posts views Thread by chaossliver | last post: by
12 posts views Thread by mohdalibaig | last post: by
5 posts views Thread by muwie | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.