public final class IoLib extends Object
The I/O library provides two different styles for file manipulation. The first one uses implicit file handles; that is, there are operations to set a default input file and a default output file, and all input/output operations are over these default files. The second style uses explicit file handles.
When using implicit file handles, all operations are supplied by table io.
When using explicit file handles, the operation io.open
returns a file handle and then all operations are supplied as methods of the file handle.
The table io also provides three predefined file handles with their usual
meanings from C: io.stdin, io.stdout, and io.stderr.
The I/O library never closes these files.
errno.| Modifier and Type | Method and Description |
|---|---|
static org.classdump.luna.runtime.LuaFunction |
file_close()
Returns the file method (a function)
close. |
static org.classdump.luna.runtime.LuaFunction |
file_flush()
Returns the file method (a function)
flush. |
static org.classdump.luna.runtime.LuaFunction |
file_lines()
Returns the file method (a function)
lines. |
static org.classdump.luna.runtime.LuaFunction |
file_read()
Returns the file method (a function)
read. |
static org.classdump.luna.runtime.LuaFunction |
file_seek()
Returns the file method (a function)
seek. |
static org.classdump.luna.runtime.LuaFunction |
file_setvbuf()
Returns the file method (a function)
setvbuf. |
static org.classdump.luna.runtime.LuaFunction |
file_tostring()
Returns the file method (a function)
tostring. |
static org.classdump.luna.runtime.LuaFunction |
file_write()
Returns the file method (a function)
write. |
static void |
installInto(org.classdump.luna.StateContext context,
org.classdump.luna.Table env,
RuntimeEnvironment runtimeEnvironment)
Installs the I/O library to the global environment
env in the state
context context. |
static org.classdump.luna.runtime.LuaFunction |
type()
Returns the function
io.type. |
public static org.classdump.luna.runtime.LuaFunction type()
io.type.
The following is the corresponding entry from the Lua Reference Manual:
io.type (obj)Checks whether
objis a valid file handle. Returns the string"file"ifobjis an open file handle,"closed file"ifobjis a closed file handle, or nil ifobjis not a file handle.
io.type functionio.typepublic static org.classdump.luna.runtime.LuaFunction file_close()
close.
The following is the corresponding entry from the Lua Reference Manual:
file:close ()Closes
file. Note that files are automatically closed when their handles are garbage collected, but that takes an unpredictable amount of time to happen.When closing a file handle created with
io.popen,file:closereturns the same values returned byos.execute.
file:close methodfile:closepublic static org.classdump.luna.runtime.LuaFunction file_flush()
flush.
The following is the corresponding entry from the Lua Reference Manual:
file:flush ()Saves any written data to
file.
file:flush methodfile:flushpublic static org.classdump.luna.runtime.LuaFunction file_lines()
lines.
The following is the corresponding entry from the Lua Reference Manual:
file:lines (···)Returns an iterator function that, each time it is called, reads the file according to the given formats. When no format is given, uses "
l" as a default. As an example, the constructionfor c in file:lines(1) do body endwill iterate over all characters of the file, starting at the current position. Unlike
io.lines, this function does not close the file when the loop ends.In case of errors this function raises the error, instead of returning an error code.
file:lines methodfile:linespublic static org.classdump.luna.runtime.LuaFunction file_read()
read.
The following is the corresponding entry from the Lua Reference Manual:
file:read (···)Reads the file
file, according to the given formats, which specify what to read. For each format, the function returns a string or a number with the characters read, or nil if it cannot read data with the specified format. (In this latter case, the function does not read subsequent formats.) When called without formats, it uses a default format that reads the next line (see below).The available formats are
- "
n": reads a numeral and returns it as a float or an integer, following the lexical conventions of Lua. (The numeral may have leading spaces and a sign.) This format always reads the longest input sequence that is a valid prefix for a numeral; if that prefix does not form a valid numeral (e.g., an empty string,"0x", or"3.4e-"), it is discarded and the function returns nil.- "
a": reads the whole file, starting at the current position. On end of file, it returns the empty string.- "
l": reads the next line skipping the end of line, returning nil on end of file. This is the default format.- "
L": reads the next line keeping the end-of-line character (if present), returning nil on end of file.- number: reads a string with up to this number of bytes, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file.
The formats "
l" and "L" should be used only for text files.
file:read methodfile:readpublic static org.classdump.luna.runtime.LuaFunction file_seek()
seek.
The following is the corresponding entry from the Lua Reference Manual:
file:seek ([whence [, offset]])Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string
whence, as follows:
"set": base is position 0 (beginning of the file);"cur": base is current position;"end": base is end of file;In case of success,
seekreturns the final file position, measured in bytes from the beginning of the file. Ifseekfails, it returns nil, plus a string describing the error.The default value for whence is
"cur", and for offset is 0. Therefore, the callfile:seek()returns the current file position, without changing it; the callfile:seek("set")sets the position to the beginning of the file (and returns 0); and the callfile:seek("end")sets the position to the end of the file, and returns its size.
file:seek methodfile:seekpublic static org.classdump.luna.runtime.LuaFunction file_setvbuf()
setvbuf.
The following is the corresponding entry from the Lua Reference Manual:
file:setvbuf (mode [, size])Sets the buffering mode for an output file. There are three available modes:
"no": no buffering; the result of any output operation appears immediately."full": full buffering; output operation is performed only when the buffer is full or when you explicitly flush the file (seeio.flush)."line": line buffering; output is buffered until a newline is output or there is any input from some special files (such as a terminal device).For the last two cases,
sizespecifies the size of the buffer, in bytes. The default is an appropriate size.
file:setvbuf methodfile:setvbufpublic static org.classdump.luna.runtime.LuaFunction file_tostring()
tostring.file:tostring methodpublic static org.classdump.luna.runtime.LuaFunction file_write()
write.
The following is the corresponding entry from the Lua Reference Manual:
file:write (···)Writes the value of each of its arguments to
file. The arguments must be strings or numbers.In case of success, this function returns
file. Otherwise it returns nil plus a string describing the error.
file:write methodfile:writepublic static void installInto(org.classdump.luna.StateContext context,
org.classdump.luna.Table env,
RuntimeEnvironment runtimeEnvironment)
env in the state
context context. The I/O functions will use the runtime environment
runtimeEnvironment.
If env.package.loaded is a table, adds the library table
to it with the key "io", using raw access.
context - the state context, must not be nullenv - the global environment, must not be nullruntimeEnvironment - the runtime environment, may be nullNullPointerException - if context or env is nullCopyright © 2016–2017. All rights reserved.