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 ?

Dani AI

Generated

A few practical points that build on the replies from , and and give concrete, safe rules to apply.

String builders start with a small internal char array (no-arg constructor uses capacity 16) and grow automatically; growth is roughly doubling the previous capacity (see the StringBuilder/AbstractStringBuilder docs). For simple counts: 10,000 characters is only about 20 KB of char storage (char = 2 bytes). Holding a few hundred kilobytes or a couple of megabytes in memory is normally fine; holding many tens of megabytes can cause noticeable GC/heap pressure and is when streaming to disk is preferable. (StringBuilder Javadoc)

A practical pattern: build in-memory up to a threshold (tune this based on available heap, e.g. 32K-256K chars), flush to a BufferedWriter, then clear the builder with setLength(0). Avoid calling toString() on very large builders because it allocates a second copy; instead copy into a char[] and call Writer.write(char[], off, len) to avoid the temporary String allocation. Example pattern:

int FLUSH_THRESHOLD = 32_768;
Path out = Paths.get("out.txt");
try (BufferedWriter bw = Files.newBufferedWriter(out, StandardCharsets.UTF_8)) {
    StringBuilder sb = new StringBuilder();
    for (String record : source) {
        sb.append(record).append('\n');
        if (sb.length() >= FLUSH_THRESHOLD) {
            char[] tmp = new char[sb.length()];
            sb.getChars(0, sb.length(), tmp, 0);
            bw.write(tmp, 0, tmp.length);
            sb.setLength(0);
        }
    }
    if (sb.length() > 0) {
        bw.write(sb.toString()); // last small chunk
    }
}

Use a BufferedWriter or Files.newBufferedWriter (modern API) so the OS sees larger writes and fewer physical disk ops (BufferedWriter, Files). Tune the flush threshold by measuring memory and throughput; for very large outputs consider streaming APIs or NIO FileChannel/mapped files to avoid keeping everything in heap.

Recommended Answers

All 4 Replies

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.