refactoring debug file write code.

add file write helper (snap file writer) that has IO exception code.