remove the writer.close() from try, only place it in the final statement