Type File

File

Methods

close() Closes the file.
flush() Writes all buffered data to the file and saves it.
lines( ... ) Returns an iterator over the file's contents, starting from the current position.
read( ... ) Reads the given file using the arguments.
seek( whence, offset ) Moves the file cursor position for reading from.
setvbuf( mode, size ) Sets the buffering mode for the given file.
write( ... ) Writes every passed in value to the given file.


Methods

File:close()
Closes the file. You cannot perform any further operations on the file once it is closed.

Environment:

Shared
File:flush()
Writes all buffered data to the file and saves it.

Environment:

Shared
File:lines( ... )
Returns an iterator over the file's contents, starting from the current position. Pass in read arguments to have them returned in the iteration.

Environment:

Shared

Parameters:

  • ...: Reading arguments to use. Defaults to "*l".

Usage:

    --Iterate over all lines. 
    for Line in File:lines() do 
    	print( Line ) 
    end 
    --Iterate over every byte. 
    for Letter in File:lines( 1 ) do 
    	print( Letter ) 
    end

Return value:

    (function) Iterator.
File:read( ... )
Reads the given file using the arguments.

Environment:

Shared

Parameters:

  • ...: Reading arguments. '*n' reads a number, '*a' reads the entire file, '*l' reads a line without the new line character, '*L' reads a line with the new line character and a number reads that many bytes.

Usage:

    local Line, Byte, Rest = File:read( "*l", 1, "*a" )

Return value:

    (any...) All read values.
File:seek( whence, offset )
Moves the file cursor position for reading from. If no arguments are provided, it returns the current position. See: http://www.lua.org/manual/5.2/manual.html#pdf-file:seek

Environment:

Shared

Parameters:

  • whence: The position in the file to seek from (one of "set", "cur" or "end").
  • offset: If whence is provided, the offset from that given position to seek to.

Return value:

    (number) The new file cursor position.
File:setvbuf( mode, size )
Sets the buffering mode for the given file. See: http://www.lua.org/manual/5.2/manual.html#pdf-file:setvbuf

Environment:

Shared

Parameters:

  • mode: The mode to use for buffering (one of "no", "full" or "line").
  • size: The size of the buffer. Applicable only if mode = "full".
File:write( ... )
Writes every passed in value to the given file.

Environment:

Shared

Parameters:

  • ...: String or number values to write to the given file.

Return value:

    (File) The file, allowing for chained calls.

Valid XHTML 1.0!