Optimised to open and close file once, rather than each write.