473,513 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8993

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1244
by: Ddraig | last post by:
Howdy, I was wondering if anyone could point me in the direction of some good code that will look at a folder of various text files, read in the names and then open them. For example opening various log files would be a good example of something like this. I think I've got the whole reading the text out of the file figured out, but just...
16
2185
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
11
2054
by: moony marouane | last post by:
Hi all I'm looking for code regarding opening (not saving) binary files (pdf, word, excel...) from hard disk with asp, the website is hosted in C:\ and the pdf files are in D:\. Does anyone have an idea about this?? Many thanks in advance. Moony.
2
4387
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take that extension and use that as my filter criteria for the File Open dialog. Once the user selects a file with that extension (from the File Open...
8
2303
by: chaossliver | last post by:
I'm a brand new scripter, and as such, I have very poor experience in coding. I have been looking around at some open source codes written in pure C and was wondering if anyone knew of a program that could convert standard C files into files visible with a Visual C++ editor, such as Microsoft's program. --Bryan
1
3034
by: celoftis | last post by:
BACKGROUND: I have some PPT slides that have been converted to HTM (ensuring that the show slide animations while browsing checkbox is checked). The original HTM slides have custom animations to allow movie (wmv) playback on mouse click. (All animations have been verified to work when viewing slides in ppt.) Further, I have ensured that the...
9
2298
compman9902
by: compman9902 | last post by:
Hello, and thank toy for reading this post. Thus far, this website has helped me a lot, (I have finally finished my encryptor: "site removed by moderator", go to the downloads link) and I will continue asking for help (so my rank may finally increase). Right now the topic at hand for me is opening file dialogs. Right now, the issue at hand is...
12
1416
by: mohdalibaig | last post by:
Is it possible to open other files in C during main() execution. Example, i want to open a wave file that generates a sound when ever main() function is called.
5
2410
by: muwie | last post by:
Hello, I was browsing to see if I could find something similair to my problem. But I couldn't find anything.. I have this script that counts every word in a file. And then also says how many times that word occurs. Now I have this directory containing about 60 text files which I need to run this script on. Seeing as I'm not really a star in...
34
5298
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> os.startfile("C:\Documents and Settings\Alex\My Documents\My
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.