Lua I/O Library Guide — File Reading, Writing, and Stream Operations
In this tutorial, you will learn about Lua I/O Library Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Lua I/O library provides simple file operations through io.open with modes (r, w, a, r+, w+, a+) and file handle methods -- supporting line-by-line, block, and formatted reading and writing.
Simple I/O
-- Read entire file
local content = io.open("input.txt", "r"):read("*all")
io.open("output.txt", "w"):write(content)
-- Read line by line
for line in io.lines("input.txt") do
print(line)
end
File Handle I/O
-- Open for reading
local f = io.open("data.txt", "r")
if f then
local line = f:read() -- read next line
local all = f:read("*all") -- read rest
f:close()
end
-- Open for writing
local f = io.open("output.txt", "w")
f:write("Hello, World!\n")
f:write(string.format("Value: %d\n", 42))
f:close()
-- Append mode
local f = io.open("log.txt", "a")
f:write(os.date() .. ": event logged\n")
f:close()
Reading Modes
local f = io.open("file.txt", "r")
local char = f:read(1) -- read 1 character
local line = f:read("*line") -- read next line
local number = f:read("*n") -- read a number
local all = f:read("*all") -- read entire file
local rest = f:read("*a") -- read all remaining
f:close()
File Operations
-- Check if file exists
local f = io.open("config.lua", "r")
if f then
f:close()
print("File exists")
end
-- File position
local f = io.open("data.bin", "rb")
print(f:seek("cur")) --> current position
print(f:seek("set", 0)) --> go to beginning
print(f:seek("end")) --> go to end (returns size)
f:close()
-- Temporary file
local f = io.tmpfile()
f:write("temporary data")
f:seek("set", 0)
print(f:read("*all")) --> temporary data
f:close()
Common Mistakes
1. Not closing files
Unclosed files cause resource leaks. Use f:close() or the with pattern.
2. Assuming read returns nil on EOF
read() returns nil at end of file. Check for nil before using the result.
3. Binary mode on Windows
On Windows, add "b" to mode ("rb", "wb") for binary files to avoid newline translation.
Practice Questions
1. How do you read a file line by line?
Use for line in io.lines("file.txt") do ... end or open a handle and call f:read() repeatedly.
2. What does io.open return on failure?
It returns nil and an error message: local f, err = io.open("nonexistent", "r").
3. How do you read a number from a file?
Use f:read("*n") which reads a number and returns it as a Lua number.
FAQ
{{< faq question="What is the difference between io.open and io.lines?" >}} io.open returns a file handle for manual control. io.lines provides a convenient Iterator that auto-closes the file. {{< /faq >}}
{{< faq question="Can I write binary data?" >}}
Yes. Use "wb" mode. Lua treats binary data as strings. Write raw bytes with f:write(string.char(0, 1, 2)).
{{< /faq >}}
{{< faq question="How do I append to a file?" >}}
Open with "a" (append) mode: io.open("file.txt", "a"). Writes go to the end of the file.
{{< /faq >}}
What's Next
Now learn about the OS library.
| Topic | Description | Link |
|---|---|---|
| OS Library | Time, date, system commands | {{< ref "21-os-library" >}} |
| Error Handling | pcall and xpcall | {{< ref "23-error-handling" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro