954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

StringBuffer() & Performances

I understand that string buffer can append data and write it in a text file. But my question is, how big or the size stringbuffer can store data? can it store more than 10000+ data? what about the performance of the system? Or should i create a if-else statement to check the amount of data that i have been append to the stringbuffer before i write it in the text file and clear the stringbuffer before continue to the next record? will this method be a best idea?Does anyone has a GREAT suggestion ?

k_en
Light Poster
35 posts since Jul 2005
Reputation Points: 13
Solved Threads: 0
 

StringBuffer is certainly better in handling lots of operations than a normal String and can handle the same number of characters as a String. It is also completely thread safe.

However if you are using JDK 5 or later, you might want to think about using a StringBuilder if system performance is your major concern. Only do this though if you can GUARANTEE that only a single thread will be accessing it at any one time (it is not thread safe).

Hope this helps,
darkagn

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

Why do you need to use a StringBuilder or StringBuffer prior to writing the data to a text file? Is there a reason you can't just use a BufferedWriter?

StringBuilder and StringBuffer are the preferred way to construct long strings from many smaller strings or characters, instead of a large number of concatenations, but if you're writing it to a file, a BufferedWriter should serve the same purpose.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

It's often preferably to collate data before writing it out to disk.
That way you can write larger chunks at once, reducing the number of physical disk operations (which improves performance).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Yes, good point, I would certainly agree there. The poster didn't give much detail on his process, so it's tough to say what the most efficient arrangement would be.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You