Lua IO Library

The io library is Lua’s built-in facility for input and output, especially file handling. It’s simple but powerful, offering everything you need for basic file I/O — reading, writing, opening, closing, and managing file streams.

Unlike LuaFileSystem (LFS), which handles directory and file metadata, the io library is focused on reading from and writing to files and standard streams like stdin, stdout, and stderr.


Importing the Library

No need to install anything — io is included in Lua’s standard library:

local io = require("io")  -- Optional; built-in by default

Opening Files

local file = io.open("example.txt", "r")  -- Read mode

Common Modes:

Mode Meaning
"r" Read (file must exist)
"w" Write (truncates file)
"a" Append (creates if needed)
"r+" Read/update
"w+" Write/update (truncates)
"a+" Append/update
"b" Binary mode (e.g., "rb")

Reading Files

Full file:

local file = io.open("example.txt", "r")
local content = file:read("*all")
file:close()
print(content)

Line-by-line:

for line in io.lines("example.txt") do
  print(line)
end

Other read formats:

Format Description
"*n" Read a number
"*l" Read a line (default)
"*a" Read entire file
number Read up to number characters

Example:

local num = file:read("*n")

Writing to Files

local file = io.open("output.txt", "w")
file:write("Hello, Lua!\n")
file:write("Another line.\n")
file:close()

Use "a" mode to append rather than overwrite.


Updating Files

To both read and write:

local file = io.open("data.txt", "r+")
local firstLine = file:read("*l")
file:seek("set", 0)  -- Go back to start
file:write("Updated line!\n")
file:close()

File Positioning with seek

file:seek("set", 0)      -- Beginning
file:seek("cur", 10)     -- Move 10 bytes forward
file:seek("end", -5)     -- 5 bytes from end

Returns the new position (in bytes).


Flushing Buffers

file:flush()

Forces buffered output to be written — useful when writing large files or log files.


Working with Standard Streams

io.write("Enter name: ")
local name = io.read("*l")
print("Hello, " .. name)

You can also use:

  • io.stdin — standard input
  • io.stdout — standard output
  • io.stderr — standard error

Temporary File

local tmp = io.tmpfile()
tmp:write("Temporary data")
tmp:seek("set")
print(tmp:read("*all"))
tmp:close()

Useful for intermediate data without affecting disk files.


Practical Example: Copying a File

local infile = io.open("source.txt", "r")
local outfile = io.open("copy.txt", "w")
outfile:write(infile:read("*all"))
infile:close()
outfile:close()

Error Handling

Always check file handles:

local file, err = io.open("maybe.txt", "r")
if not file then
  print("Error opening file:", err)
  return
end

Summary of io Functions

Function Description
io.open Open file with mode
io.close Close file (or default output file)
io.read Read from default input
io.write Write to default output
io.lines Iterator over file lines
io.input Set/get default input file
io.output Set/get default output file
io.flush Flush current output buffer
io.type Check if a file handle is open
io.tmpfile Create a temporary file