You can use the AutoIndentWriter
if you want to maintain the standard formatting.
ST template = group.getInstanceOf("YourTemplate");
OutputStreamWriter osWriter = new OutputStreamWriter(stream);
STWriter stWriter = new AutoIndentWriter(osWriter);
template.write(stWriter);
osWriter.flush();