# Send both standardOutput and errorOutput to a single file

**URL:** <https://discuss.gradle.org/t/send-both-standardoutput-and-erroroutput-to-a-single-file/14364>\
**Category:** Help/Discuss\
**Created:** [February 9, 2016, 8:26pm UTC](https://discuss.gradle.org/t/send-both-standardoutput-and-erroroutput-to-a-single-file/14364 "2016-02-09T20:26:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![awrichar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/awrichar/32/7078_2.png) [@awrichar](https://discuss.gradle.org/u/awrichar)\
**Post date:** [February 9, 2016, 8:26pm UTC](https://discuss.gradle.org/t/send-both-standardoutput-and-erroroutput-to-a-single-file/14364/1 "2016-02-09T20:26:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Strongbeard](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Strongbeard](https://discuss.gradle.org/u/Strongbeard)\
**Post date:** [October 7, 2022, 4:46pm UTC](https://discuss.gradle.org/t/send-both-standardoutput-and-erroroutput-to-a-single-file/14364/2 "2022-10-07T16:46:29Z")

</div>

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.
