close file and connection before exit.