0

For a web project I'm writing large sections of text to a webpage(table) or even bigger (could be several MB) to CSV files for download. The java method dealing with this receives a StringBuilder content string, which originally (by the creator of this module) was being sent char by char in a loop:

response.getOutputStream().write(content.charAt(i)).

Upon questioning about the loop, the reason given was that he thought the string might be too big for writing in one go. (using java 1.6). I can't find any size restrictions anywhere, and then also the question came which method to use instead: print() or getWriter()? The data in the string is all text.

mljm
  • 327
  • 3
  • 13

1 Answers1

1

He assumed wrong. If anything it's inefficient, or at least useless to do that one character at a time. If you have a String in memory, you can write it out at one go without worrying.

If you're only writing text, use a Writer. OutputStream is for binary data (although you can wrap it in an OutputStreamWriter to convert between the two). See Writer or OutputStream?

Kayaman
  • 72,141
  • 5
  • 83
  • 121