Binary output

If you’re dealing with large amounts of data, typically, you don’t want to write
big ASCII files. You want to write things as binary file (it takes less space to do that).
In most programming language that’s easy but is there a way to write out (read) the numbers directly as binary in Julia?

Several, including plain old mmaping,

https://docs.julialang.org/en/v1/stdlib/Serialization/

and some others. Which one is best for you depends on your requirements. Search for past dicussions.

1 Like

In addition, if you are dealing with some esoteric format not mentioned above, you can also simply write values directly into a file:

julia> open("foo.dat", "w") do file
         write(file, 1) # write an integer
         write(file, 1.0) # write a Float64
       end
8

The above command produces a file with exactly 16 bytes (8 bytes for an Int64 and another 8 for a Float64):

shell> stat foo.dat
  File: foo.dat
  Size: 16              Blocks: 8          IO Block: 4096   regular file

Note that doing your binary output this way requires careful attention to things like endian-ness, so using an established binary format like HDF5, BSON, MsgPack, or JLD2 will likely make your life much easier in the long term.

3 Likes

Thank you