How do I write an array of Int8 to disk exactly

I want to create a file that on the disk is exactly the value of a given arrayof UInt8.

Say I have generated a arrayof length n and I gave it the name seq.

Does the following do what I want?

f = open("filename",w)
write(f, seq)

Are there any considerations about the length n? Is there any padding when the file is written on the disk?

If the arrayhas length 1, the file will be 1 byte, but will it occupy only 1 byte on the disk?

Juia’s write is “what you see is what you get”. It behaves the same way whether you are writing to a file, a buffer in memory, or any other stream. So no, no padding or any other type of formatting, it just writes exactly what you tell it to. Granted, I’m not 100% sure how it would decide to serialize everything, but as far as I know it throws an error for anything that doesn’t have an entirely straightforward serialization. Note also that you can use it in conjunction with the stdlib Serialization.

Note also that you can of course verify the behavior with open(read, "filename") or Mmap.mmap("filename"). What is returned is an unformatted Vector{UInt8}, so in your case you’d have to do reinterpret(UInt8, Mmap.mmap("testfile")) or similar.


That’s okay but can be shortened a little if you want:

julia> write("/tmp/testdata", rand(UInt8, 10))

shell> ls -l /tmp/testdata
-rw-rw-r-- 1 gunnar gunnar 10 okt 21 22:02 /tmp/testdata

That really is up to file system on the disk to decide and well outside Julia’s control.

1 Like

Thanks. Your reply was very useful. However, I think that GunnarFarneback actually answered my (rather silly) question. It does depend on the file system.