O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Streaming a file as a resource

In the Generating lazy (even infinite) sequences recipe, it was possible to understand the difference between eager and lazy evaluation, namely the use of Enum or Stream modules.

When working with files, it is possible to load all of the file's contents into the memory (File.read/1 or File.read!/1), or the file might be read a line or n bytes at a time (File.stream!/3). Using the File.stream! function allows you to work with really large files that might not fit the available memory.

In this recipe, we will read text from a file and output an uppercased version into a new file.

Getting ready

Start an IEx session and make sure you have stream_file.txt in a known location:

> iex

How to do it…

To read a file one line

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required