Make sure file is closed.

Add defer statement to make sure the file is closed after it has been used.