Send both standardOutput and errorOutput to a single file

When defining an Exec task, is it possible to redirect both standardOutput and errorOutput to the same file, so they are interleaved as they happen? I tried this:

def logFile = file("${temporaryDir}/output.txt")
standardOutput = logFile.newOutputStream()
errorOutput = standardOutput

This sometimes seems to work and sometimes fails with java.io.IOException: Stream Closed. For now I’m sending the two streams to two separate files, but is there any (simple) way to use only one?

Instead of using java.io.OutputStream, try using java.io.FileOutputStream. It overrides its finalize() method to only call close() after all references to it are removed.