Julia File I/O Guide — Reading, Writing, and File Operations
In this tutorial, you will learn about Julia File I/O Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia file I/O uses open() with do-block for automatic resource management, read() for entire content, eachline() for line iteration, and CSV.jl/DataFrames.jl for tabular data -- with write(), append(), and FileIO.jl for various formats.
Basic File Reading
# Read entire file as string
content = read("file.txt", String)
# Read lines into array
lines = readlines("file.txt")
# Iterate line by line (lazy)
for line in eachline("file.txt")
println(line)
end
File Writing
# Write string
write("output.txt", "Hello, Julia!\n")
# Write array
lines = ["line 1", "line 2", "line 3"]
write("output.txt", join(lines, "\n"))
# Append
open("log.txt", "a") do io
write(io, "New log entry\n")
end
# Write with formatting
using Printf
open("data.txt", "w") do io
@printf(io, "Value: %.2f\n", 3.14159)
end
File Handle Operations
# Open with do-block (auto-close)
open("file.txt", "r") do io
for line in eachline(io)
println(line)
end
end
# Manual open/close
io = open("file.txt", "r")
try
data = read(io, String)
finally
close(io)
end
# File position
io = open("data.bin", "r")
seek(io, 0) # beginning
seekend(io) # end
position(io) # current position
skip(io, 100) # skip bytes
CSV Files
using CSV
using DataFrames
# Read CSV
df = CSV.File("data.csv") |> DataFrame
# Read with options
df = CSV.read("data.csv", DataFrame;
delim = ',',
header = 1,
types = Dict(:age => Int, :name => String)
)
# Write CSV
CSV.write("output.csv", df)
# Write with options
CSV.write("output.csv", df; delim = '|')
Common Mistakes
1. Forgetting to close files
Use do-blocks (open(f -> ..., "r")) for automatic closing. Manual open requires explicit close in a try/finally block.
2. Path issues
Use joinpath("dir", "file.txt") for platform-independent paths. Use @__FILE__ and dirname for relative paths.
3. Encoding errors
read("file.txt", String) assumes UTF-8. For other encodings, use read("file.txt", String, enc"ISO-8859-1").
Practice Questions
1. How do you read a file line by line?
for line in eachline("file.txt") iterates without loading the entire file into memory.
2. How do you write to a file?
write("output.txt", content) or use open("f", "w") do io; write(io, data); end.
3. How do you read a CSV file?
using CSV; df = CSV.read("file.csv", DataFrame) loads into a DataFrame.
FAQ
{{< faq question="What file modes does open support?" >}} "r" (read), "w" (write/truncate), "a" (append), "r+" (read/write), "w+" (read/write/truncate). {{< /faq >}}
{{< faq question="How do I handle binary files?" >}}
Use read("file.bin") which returns Vector{UInt8}. Write with write("file.bin", data::Vector{UInt8}).
{{< /faq >}}
{{< faq question="What is the difference between read and readlines?" >}} read reads the entire file as a single string. readlines reads into an array of strings (one per line). {{< /faq >}}
What's Next
Now learn about plotting in Julia.
| Topic | Description | Link |
|---|---|---|
| Plotting | Data visualization | {{< ref "13-plotting" >}} |
| DataFrames | Tabular data handling | {{< ref "14-dataframes" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro