A few ways to read files with Elixir

Robson William
3 min readNov 11, 2020

Sometimes in our dev lives all we want is a single and straightforward way to solve a problem. Specially when we are hard pressed to deliver a new feature or to solve a bug.

However, as curious developers, we love to learn and get to know how computers and programs work. And so, most of the time we really want to know all the different ways we can resolve a problem.

In this post we will explore a few different ways to read files using Elixir. The end goal is to expand and deepen our understanding of Elixir and programming. Let’s jump into it!

File.read/1

As described in the official documentation, the File module contains functions to manipulate files. Just by its description, this module sounds like the obvious choice for our purpose here. The read function returns {:ok, binary} if successful, where binary is a binary data object that contains the contents of path. Or this function returns {:error, reason} if an error occurs.

Here is an example of this module and function in use:

The above code is the content of an Elixir script, file_reader.exs. In this script we define a module called “FileReader” using the defmodule keyword. This module has only one function, “read”, which is created with the keyword read. All this function does is call the read function from the File module, passing a file path as argument. Finally, the function writes the content of the file to STDOUT.

To run this code as a script, simply save it in a file with the extension “.exs” and run it like this:

$ elixir my_script.exs

File.read!/1

Another options is using the bang version of the read function. The difference is that this version raises an error if there is a problem, and the version without ! returns an atom that can be used for pattern matching.

File.open/2

This functions “opens the given ‘path’” using a given “mode”. In the example below, our test.txt file is opened with read mode. After the file is opened and the returned values match {:ok, file} we use the IO.read/2 function to read the content of the file and then we send that value to IO.puts/1 which prints that to STDOUT. Finally, we simple close the file.

More about the File module

As described in the official Elixir Getting Started guide, “the File module contains functions that allow us to open files as IO devices. By default files are opened in a binary mode, which requires developers to use the specific IO.binread/2”.

Another interesting thing about this module is that it contains several functions to interact with files, and these functions are named after their UNIX equivalents, such as File.rm/1 and File.mkdir/1.

References:

IO and the file system — The Elixir programming language (elixir-lang.org)

File — Elixir v1.11.2 (hexdocs.pm)

IO — Elixir v1.11.2 (hexdocs.pm)

--

--

Robson William

Software developer. C# and Go. Enthusiast for gRPC and IoT. Also husband, father and believer in Christ. https://robwillup.com