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 ?

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

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.