write a stream to a file