Fix a potential resource leak

A FileOutputStream was not closed in case of an exception