Dynamically choose write function depending on filetype.