new File
- create a new file handle
file = new File(
name,
mode)
The new File
syntax is used to create an OADL
file handle associated with name. If name is
nil
then an unnamed temporary file will be
created.
The mode string specifies information about how name
is to be connected to the resulting file handle. The first character
of mode may be the letter 'p'
, which
indicates that name is actually a command line for an external
program (this is not supported on all operating systems). The next
character of mode (or the first character, if
mode[0]
is not 'p'
)
specifies whether file will be connected with an existing file
or will be created as a new file:
Char | Description |
---|---|
'r' |
Open an existing file for reading |
'w' |
Create a new file or truncate an existing file for writing |
'a' |
Open an existing file or create a new file for appending |
The default for new files is text-mode processing; this may be adjusted with subsequent characters of mode
Char | Description |
---|---|
'b' |
Open the file in binary mode |
'+' |
Open the file for both reading and writing |
Any other characters of mode are prohibited. Note that file handles are not garbage collected; they must be explicitly closed when they are no longer needed.
Upon success, a new File
handle is created and
returned. Note that the return value may be lexicographically
identical to the return value of a prior new
File()
call if that file was closed.
ArgCheck
for an incorrect number of arguments
TypeCheck
for a name or mode
that is not a string
AccessCheck
for
attempts to open non-existent files for mode
"r"
or
"a"
IoCheck
for other miscellaneous I/O errors (see ferror)
// Use the system command "ls" as a pipe file = new File("ls", "pr") while (true) { try { var s = file.readstr(); "", s, '\n'; } catch (e) { break; } } file.close()