SPARK-1656: Fix potential resource leaks

JIRA: https://issues.apache.org/jira/browse/SPARK-1656
I noticed that `outputStream.close` also may throw IOException. Therefore I changed to this approach to make sure if any exception is thrown, we can always try to delete the file.