io - io action (extended)#
Adapted Air780E
Note
This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!
Tip
This library has its own demo,click this link to view io demo examples
Example
-- io module is native to lua, luatOS adds some API
-- please use with OS module
-- read-only mode, opening files
local fd = io.open("/xxx.txt", "rb")
-- Read-write default, open file
local fd = io.open("/xxx.txt", "wb")
-- Write to file and truncate to 0 bytes
local fd = io.open("/xxx.txt", "wb+")
-- Append mode
local fd = io.open("/xxx.txt", "a")
-- If the file is opened successfully, fd is not nil, otherwise it is a failure.
-- Note that the files added during the refresh are all in the/luadb directory and read-only
if fd then
-- Read the specified number of bytes, and if the data is insufficient, only the actual length of the data is returned.
local data = fd:read(12)
-- Read by row
local line = fd:read("*l")
-- Read All
local line = fd:read("*a")
-- data writing, only w or a mode can be called
-- The data needs to be a string. Lua's string has a length and can contain any binary data.
fd:write("xxxx")
-- The following is written 0x12, 0x13
fd:write(string.char(0x12, 0x13))
-- move handle, absolute coordinates
fd:seek(1024, io.SEEK_SET)
-- Move handle, relative coordinates
fd:seek(1024, io.SEEK_CUR)
-- Move the handle, reverse the absolute coordinates, from the end of the file to the head of the file.
fd:seek(124, io.SEEK_END)
-- After performing the operation, be sure to close the file.
fd:close()
end
io.exists(path)#
Determine whether the file exists
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
File Path |
Return Value
return value type |
explanation |
---|---|
bool |
Existence returns true, otherwise false |
Examples
log.info("io", "file exists", io.exists("/boottime"))
io.fileSize(path)#
Get File Size
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
File Path |
Return Value
return value type |
explanation |
---|---|
int |
File data, if the file does not exist will be returned nil |
Examples
local fsize = io.fileSize("/bootime")
if fsize and fsize > 1024 then
log.info("io", "file size", fsize)
end
io.readFile(path, mode, offset, len)#
Read the entire file, note the memory consumption
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
File Path |
string |
Read mode, default “rb” |
int |
starting position, default 0 |
int |
Read length, default entire file |
Return Value
return value type |
explanation |
---|---|
string |
File data, if the file does not exist will be returned nil |
Examples
local data = io.readFile("/bootime")
-- Note: The offset and len parameters were added in 2023.6.6
-- Read abc.txt, skip 128 bytes first, then read 512 bytes of data
local data = io.readFile("/abc.txt", "rb", 128, 512)
io.writeFile(path, data)#
Write data to a file
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
File Path |
string |
Data |
Return Value
return value type |
explanation |
---|---|
boolean |
Returns true on success, otherwise false |
Examples
io.writeFile("/bootime", "1")
io.fill(buff, offset, len)#
Read the file and fill it into the zbuff, but do not move the pointer position
Parameters
Incoming Value Type |
Explanation |
---|---|
userdata |
zbuff Entity |
int |
The location to write to, the default is 0 |
int |
The written length, which is minus the len of zbuff by default.offset |
Return Value
return value type |
explanation |
---|---|
boolean |
Returns true on success, otherwise false |
int |
Returns the length actually read. If it is less than 0, the read failed. |
Examples
local buff = zbuff.create(1024)
local f = io.open("/sd/test.txt")
if f then
f:fill(buff)
end
io.mkfs(path)#
To format a file system, you must specify a mount point.
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
Mount Point |
Return Value
return value type |
explanation |
---|---|
bool |
Success or not |
int |
underlying return value |
Examples
local ret, errio = io.mkfs("/sd")
log.info("fs", "mkfs", ret, errio)
io.mkdir(path)#
Create Folder
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
The directory path that needs to be created |
Return Value
return value type |
explanation |
---|---|
bool |
Success or not |
int |
underlying return value |
Examples
local ret, errio = io.mkdir("/data/")
log.info("fs", "mkdir", ret, errio)
io.rmdir(path)#
Delete Folder
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
Directory path to remove |
Return Value
return value type |
explanation |
---|---|
bool |
Success or not |
int |
underlying return value |
Examples
local ret, errio = io.rmdir("/data/")
log.info("fs", "rmdir", ret, errio)
io.lsdir(path, len, offset)#
List files in a directory
Parameters
Incoming Value Type |
Explanation |
---|---|
string |
The directory path that needs to be enumerated |
int |
Maximum length, default 10, maximum 50 |
int |
Offset, default 0, used for paging queries when there are many directory files. |
Return Value
return value type |
explanation |
---|---|
bool |
Success or not |
int |
underlying return value |
Examples
local ret, data = io.lsdir("/data/", 10, 0)
if ret then
log.info("fs", "lsdir", json.encode(data))
else
log.info("fs", "lsdir", "fail", ret, data)
end
io.lsmount()#
List all mount points
Parameters
None
Return Value
return value type |
explanation |
---|---|
table |
Mount point list |
Examples
local data = io.lsmount()
log.info("fs", "lsmount", json.encode(data))