Class fs.File
The File class represents an open file returned by fs.open.
As a rule of thumb, you should open files with fs.open instead of io.open because the latter doesn’t support the Virtual File System.
Methods
fs.File:close() | Closes the file. |
fs.File:flush() | Saves any written data to the file. |
fs.File:lines([what]) | Returns an iterator over the file’s contents. |
fs.File:read([what]) | Reads from file. |
fs.File:seek([whence[, offset]]) | Seeks in file. |
fs.File:setvbuf(mode[, size]) | Sets the buffering mode for an output file. |
fs.File:stat([...]) | Stats the file. |
fs.File:sysread(count) | Reads from file, non-buffered. |
fs.File:syswrite(s) | Writes to file, non-buffered. |
fs.File:write(...) | Writes to file. |
Methods
- fs.File:close()
-
Closes the file.
See Lua’s file:close
- fs.File:flush()
-
Saves any written data to the file.
See Lua’s file:flush
- fs.File:lines([what])
-
Returns an iterator over the file’s contents.
See Lua’s file:lines
- fs.File:read([what])
-
Reads from file.
See Lua’s file:read
The only difference is that the “*n” format isn’t supported.
- fs.File:seek([whence[, offset]])
-
Seeks in file.
See Lua’s file:seek
- fs.File:setvbuf(mode[, size])
-
Sets the buffering mode for an output file.
See Lua’s file:setvbuf
- fs.File:stat([...])
-
Stats the file.
Similar to fs.stat.
local f = assert(fs.open("/etc/fstab")) local s = f:stat() print(s.blksize) -- 4096 (for example)
- fs.File:sysread(count)
-
Reads from file, non-buffered.
This function reads from the file directly instead of using a buffer like read does.
In other words, this function is like Unix’s read(2) whereas read is like Unix’s fread(3).
Return value: see that of fs.filedes.read.
- fs.File:syswrite(s)
-
Writes to file, non-buffered.
This function writes to the file directly instead of using a buffer like write does.
In other words, this function is like Unix’s write(2) whereas write is like Unix’s fwrite(3).
Return value: see that of fs.filedes.write.
- fs.File:write(...)
-
Writes to file.
See Lua’s file:write
(Because of buffering you may have to call flush to actually see the data written out.)