Binary files can actually be written and read the same way as text files. The binary files are, of course, not readable in a text editor, but can easily be read back into AX.
The only change you have to make to the two previous examples is to use the BinaryIo class instead of the TextIo class.
The use of binary files to export data from AX is rare as the data will only be readable from AX. You can use it if you don't want the exported data to be readable outside AX.