# How do I write an array of Int8 to disk exactly

**URL:** https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143
**Category:** General Usage
**Created:** [October 21, 2019, 6:21pm UTC](https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143 "2019-10-21T18:21:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [October 21, 2019, 6:21pm UTC](https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143/1 "2019-10-21T18:21:47Z")

</div>

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?

```julia
f = open("filename",w)
write(f, seq)
close(f)

```

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?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [October 21, 2019, 6:28pm UTC](https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143/2 "2019-10-21T18:28:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 21, 2019, 8:04pm UTC](https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143/3 "2019-10-21T20:04:49Z")

</div>

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

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

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

```

> [@gmouts](#):
>
> If the arrayhas length 1, the file will be 1 byte, but will it occupy only 1 byte on the disk?

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

---

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [October 24, 2019, 11:11pm UTC](https://discourse.julialang.org/t/how-do-i-write-an-array-of-int8-to-disk-exactly/30143/4 "2019-10-24T23:11:33Z")

</div>

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