File type

Represents a file in a file system (S_IFREG)

Direct parent types

Path
Represents file system path such as a file, a block device, etc
Direct subtypes: 7

Direct children types

MaybeFile
Like File but the programmer is aware that it might not exist. Therefore, no exceptions will be thrown for most operations on the file if it is not present.
TmpFile
Represents a temporary file. Automatically removed when the script exits.

Methods

access(f:File, mode:Int=0) Source: stdlib.ngs:8521
Check whether current process has the specified access to the file. Wrapper around ACCESS(2). Throws CError.

Parameters

modeOne of the file ACCESS::* modes, defaults to ACCESS::F_OK (test file existence only).

Returns

Bool

Example

access(File("mydata"), ACCESS::R_OK).not() throws Error("No access to data file").set("file", "mydata")
close(f:File) Source: stdlib.ngs:6279
Close a file and sets the "fd" field to null. Uses CLOSE(2). Throws InvalidArgument if file is not open. Throws FileIOFail if an underlying error occurs.

Returns

f
echo(f:File, s:Str) Source: stdlib.ngs:8570
Write a string followed by newline character to a File. Throws WriteFail on failure.

Returns

null

Example

echo(File("1.txt"), "blah")
echo(f:File, l:Lines) Source: stdlib.ngs:8579
Write a lines followed by newline character to a File. Throws WriteFail on failure.

Returns

null

Example

echo(File("1.txt"), Lines(["aaa", "bbb"]))
init(pr:ProcessRedir, fd:Any, marker:Any, datum:File) Source: stdlib.ngs:5984
Undocumented
init(f:File, path:Str) Source: stdlib.ngs:6190
Create File object from the given path. The file is not open.

Example

f = File('/tmp/myfile')
init(f:File, fd:Int) Source: stdlib.ngs:6196
Create File object from the given file descriptor.
init(f:File, path:Path) Source: stdlib.ngs:6200
Undocumented
lines(f:File, cb:Fun) Source: stdlib.ngs:6239
Iterate over lines of the file

Parameters

fClosed File
cbfunction to call with successive lines from the file
lines(f:File) Source: stdlib.ngs:6256
Get all lines of the file

Returns

Arr of Str
lines(f:File, lines_:Arr) Source: stdlib.ngs:6261
Write given lines to the file. Overwrites the file.

Returns

Arr of Str
open(f:File, flags:Str) Source: stdlib.ngs:6208
Open a file and set the "fd" field to the file descriptor. Uses OPEN(2). Throws InvalidArgument if file is already open. Throws FileIOFail if an underlying error occurs.

Parameters

flagsCurrently "r", "w" or "a" for read/write/append

Returns

f
read(f:File) Source: stdlib.ngs:8541
Read whole file
replace(f:File, src:Any, dst:Any) Source: stdlib.ngs:5932
Replace in a file. Wrapper around replace(Str, Any, Any).

Returns

f
store(file:File, data:Any, encode_hints:Hash={}) Source: stdlib.ngs:6327
encode() and write() the data to the file
write(s:Str, f:File) Source: stdlib.ngs:8557
Write whole file if it's closed, write to file descriptor if it's open
write(f:File, s:Str) Source: stdlib.ngs:8561
Write whole file if it's closed, write to file descriptor if it's open