copy in the file writing code from the example