Do not forget to close a file after the transaction on this one ended. This is done automatically when the
Fileobject is garbage collected, but that cannot be predicted.
By Samir Tine, published on December 2021
In the Lua standard library, input / output functionalities are gathered within the
In Luart, they are grouped together within the sys module, and more particularly the File object.
At this point, it's important to understand that a File object represents an abstraction of a file that may *or may not* physically exist.
For example, when creating a new file, only the call to the File:open() method will physically create the file on the computer storage:
local newfile = sys.File("hello.txt") -- newfile holds a File value print(newfile.exists) -- outputs false newfile:open("write") -- creates the file physically on the disk newfile:close() -- closes the opened File print(newfile.exists) -- now outputs true
Once created, a File instance can be used to perform file operations:
local file = sys.File("test.txt") file:copy("test_copy.txt") -- copies file file:move("C:\") -- moves file file:remove() -- deletes the file
A File can be opened to read or write its content. This is done by calling the File:open() method. By default, the file is open in
"read" mode. The file encoding can be specified as a second argument or autodetected using Byte Mark Order :
local file = sys.File("test.bin") -- opens the file "test.bin" in read mode (encoding is autodetected) file:open() -- closes the file file:close() -- open the file in write mode (erasing its previous content), using "binary" encoding(ie raw binary) file:open("write", "binary")
File:open()method called, you can write and/or read on the File (depending on the open mode used). These methods return Buffer objects in
"binary"mode (containing in memory binary data) or strings in
local file = sys.File("README.TXT"):open() -- opens the File for reading print(file:read()) -- reads and prints all the file content
File:read()method reads a certain number of characters (when using
"unicode"mode) or bytes (in
"binary"mode) specified as argument, or all the content of the file if no argument is provided.
File:readln()method reads a line, until end of line characters "\r\n" or "\n" are encountered. You can iterate through lines of the file with the File.lines iterator.
"binary"mode, Buffer objects can be used.
File:writeln()writes a line to the file, appending "\r\n" (the End Of Line marker on Windows platform).
"binary"encoding : the position is expressed in bytes.
"unicode"encoding : the position is expressed in characters.
-- Create File instance representing the Windows notepad.exe executable local file = sys.File(sys.env.WINDIR.."/notepad.exe") -- prints when notepad.exe was created on your system print(file.created)
-- Create File instance representing the Windows notepad.exe executable local file = sys.File(sys.env.WINDIR.."/notepad.exe") -- prints if notepad.exe is readonly (should be false) print(file.readonly)
local file = sys.File("test.lua") if file.extension == ".lua" then print("It's a Lua script file !") else print("Don't know what kind of file it is...") end
As you can see, File objects provide easy access to all of the functionality related to creating, reading, writing and manipulating files in Luart.
These features go beyond the capabilities offered by the standard Lua library on the Windows platform.