Lua:
Reading a text file

How to:

Let’s check out how to read a text file line by line and then all at once.

-- Read file line by line
local file = io.open("example.txt", "r") -- Open the file for reading
if file then
  for line in file:lines() do -- Iterating over each line in the file
    print(line)
  end
  file:close() -- Always close the file when you're done
else
  print("Cannot open file.")
end

-- Read the entire file at once
local file = io.open("example.txt", "r") -- Open the file for reading
if file then
  local content = file:read("*a") -- Read the entire content
  print(content)
  file:close() -- Close the file
else
  print("Cannot open file.")
end

Sample output for both snippets, if example.txt contains:

Hello, Lua!

The output will be:

Hello, Lua!

Deep Dive

Historically, reading files is a fundamental operation, dating back to early computers. In Lua, this is handled via simple I/O model with the io library.

While io.lines and io.read are common ways to access a file’s content, there are alternatives like lfs (LuaFileSystem) for advanced file operations.

When reading, Lua handles buffering behind the scenes, yet for large files, you should read in chunks to avoid high memory usage.

Using the io library is straightforward, but always remember to close files to prevent resource leaks. On error, Lua file operations return nil and an error message, which you should handle for robustness.

See Also: