Elixir basics · Lesson 19 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · INTERMEDIATE · LESSON 1925 minutes

Read and write one real file

Build a path, write UTF-8 text, and handle the tagged read result.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Write and read one temporary UTF-8 file
path = Path.join(System.tmp_dir!(), "beam-path-note.txt")
:ok = File.write(path, "north
south
")
{:ok, text} = File.read(path)
{path, String.split(text, "
", trim: true)}
Check the result first
  • The tuple contains the temporary path and ["north", "south"].
02 · Take the code apart

Read from the first line down

  1. Read Path.join/2: Join path segments for the current operating system.
  2. Read File.write/2: Write bytes and return a tagged result.
  3. Read File.read/1: Read the file and return tagged data.
03 · Meet the new symbols

Symbols are not secret signs

Path.join/2

Join path segments for the current operating system.

File.write/2

Write bytes and return a tagged result.

File.read/1

Read the file and return tagged data.

04 · Ideas inside the code

Match each name to its meaning

01

path

Path.join/2 builds a platform-safe path instead of joining strings by hand.

02

tagged file result

Non-bang File functions return {:ok, data} or {:error, reason}.

03

resource boundary

Files belong outside pure transformation functions so errors and cleanup stay visible.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

Path.join/2 builds a platform-safe path instead of joining strings by hand.

Non-bang File functions return {:ok, data} or {:error, reason}.

06 · Change it yourself

Close the answer and try

Append east to the same file and read it again.

Practice starting point
:ok = File.write(path, "east
", ____)
File.read(path)

Target result: Use append mode and receive {:ok, text}.

Stuck? Read one hint

Pass [:append] as the third argument.

After you run it, see one answer
One answer
:ok = File.write(path, "east
", [:append])
File.read(path)
Think about it: Why use `Path.join` instead of a hard-coded slash?

It builds a valid path on different operating systems.

Take with you

Remember these three lines

  1. 1

    Build paths with Path.

  2. 2

    Keep file errors tagged.

  3. 3

    Separate file access from pure data cleanup.

Lesson completeRun the code once, then mark this lesson as done.