Lua - Reading File Line by Line



In Lua, we can read a file line by line easily. In this chapter we'll explore multiple ways to read a file.

  • io.read([mode]) - Using simple model, we can use io.read() method to read a file line by file. Each invocation of io.read() method returns next line read from the file. mode is an optional parameter. We can specify mode as "*line", to read file line by line.

  • io.lines() - Using simple model, we can use io.lines() method to read a file line by file. io.lines() returns a iterator to traverse through all lines of the file.

  • file:read([mode]) - Using complete model, we can use file:read() method to read a file line by file where file represent the file handle returned by io.open() method. Each invocation of file.read() method returns next line read from the file. mode is an optional parameter. We can specify mode as "*line", to read file line by line.

Let's explore each of the above methods to read a file line by line.

we will use a sample file example.txt as shown below−

example.txt

Welcome to tutorialspoint.com
Simply Easy Learning

Example - Read File line by Line using io.read()

Let us now see how to read a file line by line using io.read() method call.

main.lua

-- read a file content and returns the same
function readFile()
   -- Opens a file in read
   f = io.open("example.txt","r")
   -- set f as default input
   io.input(f)
   -- read first line
   print(io.read())
   -- read next line
   print(io.read())
   -- close the file handle
   io.close(f)
   -- return the contents
   return contents
end

-- read the file
readFile()

Output

When the above code is built and executed, it produces the following result −

Welcome to tutorialspoint.com
Simply Easy Learning

Example - Read File line by Line using f.read()

Let us now see how to read a file line by line using f.read("*line") method call.

main.lua

-- read a file content and returns the same
function readFile()
   -- Opens a file in read
   f = io.open("example.txt","r")
   -- read first line
   print(f:read(("*line")))
   -- read next line   
   print(f:read("*line"))
   -- close the file handle
   f:close()
   -- return the contents
   return contents
end

-- read the file
readFile()

Output

When the above code is built and executed, it produces the following result −

Welcome to tutorialspoint.com
Simply Easy Learning

Example - Read File line by Line using io.lines()

Let us now see how to read a file line by line using io.lines() method call.

main.lua

-- read a file content and returns the same
function readFile()
   -- Opens a file in read
   f = io.open("example.txt","r")
   -- set f as default input
   io.input(f)

   -- traverse through each lines
   for line in io.lines() do
      print(line)
   end

   -- close the file handle
   io.close(f)
   -- return the contents
   return contents
end

-- read the file
readFile()

Output

When the above code is built and executed, it produces the following result −

Welcome to tutorialspoint.com
Simply Easy Learning
Advertisements