Skip to content

Tag: stringbuilder

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder? Answer Mutability Difference: String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Differenc…

Dumping a Java StringBuilder to File

What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: But is this efficient for a very long file? Is there a better way? Answer As pointed out by others, use a Writer, and use a BufferedWriter, but then don’t call writer.write(stringBuilder.toString()); instead just …