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.

This class is a superset of Lua’s own file object and you use it in the same way. Therefore many of the entries here link to Lua’s manual.

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.

The name comes from the Perl (and Ruby) function by the same name.

Caveat: Don’t mix buffered I/O with unbuffered I/O.

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.

The name comes from the Perl (and Ruby) function by the same name.

Caveat: Don’t mix buffered I/O with unbuffered I/O.

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.)

generated by LDoc 1.4.3 Last updated 2016-08-23 17:29:40