How to read first 60 bytes of a large file?

Hello, guys.

I wanna read first 60 bytes of a large file.

Currently, I have tried!/1 and!/3.!/1

It reads entire files. Not what I need.!/3

I saw the official example:

# Read in 2048 byte chunks rather than lines!("./test/", [], 2048)
#=> %File.Stream{line_or_bytes: 2048, modes: [:raw, :read_ahead, :binary],
#=>   path: "./test/", raw: true}

Even if it reads one chunk whose size is 2048 byte every time, It always streams entire file.


How to read the first 60 bytes of a large file?

1 Like

Perhaps you are looking for IO.binread/2.

Failing that look at


To be fair, you can still use the even if you don’t want the whole file.!("./test/", [], 60)
|> Enum.take(1)

gives you a list of a single binary of 60 bytes and doesn’t read the rest of the file.

But the tips you got already also work.


Thanks! @peerreynders @jola :grin: