write file check and finally