Cleaned up resources in writing to file

file ouputstream was still open