Fix possible resource leak

by ensuring that FileOutputStream is closed.