Read file in chunks

123 views
Skip to first unread message

Alexander

unread,
May 5, 2016, 10:10:37 AM5/5/16
to elixir-lang-talk
Is there a good way of reading big file in chunks of N lines?

At this moment it works this way (per line):

stream = File.stream!(file.path, [:read, :utf8], :line)
Enum.each stream, fn(line) ->
  # process line
end


Alexander

unread,
May 5, 2016, 10:37:17 AM5/5/16
to elixir-lang-talk
Self-answer:

It can be done using Stream.chunk
Key point i missed early is last optional param, with it it does process incomplete chunks correctly

Stream.chunk(stream, n, n, [])
Reply all
Reply to author
Forward
0 new messages