Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • 1
    That was just an example to create some fake "records" — I would assume that in the real world the records are coming from somewhere else (a database in the OP's case). But yes, if you need to read all the content into memory first, a StringBuffer would probably be faster. A raw String array (String[]) would also probably be faster. Commented Nov 5, 2020 at 18:13
  • Using StringBuffer will waste lots of resources. Most standard java writers use StreamEncoder internally and it has its own buffer of 8192 bytes. Even if you create one String of all the data it is going in as chunks and being encoded from chars to byte[]. The best solution would be implement own Writer which directly uses write(byte[]) method of FileOutputStream which used underlying native writeBytes method . Commented Apr 11, 2021 at 8:57
  • like @DavidMoles said source format of data is also very important in this scenario. If data is already available in bytes write directly to FileOutputSteam. Commented Apr 11, 2021 at 9:01