This class provides methods for synchronous operations on files and directories. For asynchronous operations, see nusys.async.FileSystem.

Passing null as a path to any of the functions in this class will result in unspecified behaviour.

Static variables

@:value(nusys.async.FileSystem)staticinlineread onlyasync:FileSystem> = nusys.async.FileSystem

Static methods

staticabsolutePath (path:String):String

Deprecated:

Available on cross

@:value({ mode : FileAccessMode.Ok })staticaccess (path:FilePath, mode:FileAccessMode = FileAccessMode.Ok):Void

Tests specific user permissions for the file specified by path. If the check fails, throws an exception. mode is one or more FileAccessMode values:

Mode values can be combined with the bitwise or operator, e.g. calling access with the mode:

FileAccessMode.Execute | FileAccessMode.Read

will check that the file is both readable and executable.

The result of this call should not be used in a condition before a call to e.g. open, because this would introduce a race condition (the file could be deleted after the access call, but before the open call). Instead, the latter function should be called immediately and errors should be handled with a try ... catch block.

staticappendFile (path:FilePath, data:Bytes, ?flags:FileOpenFlags, ?mode:FilePermissions):Void

Available on cross

Appends data at the end of the file located at path.

@:value({ followSymLinks : true })staticchmod (path:FilePath, mode:FilePermissions, followSymLinks:Bool = true):Void

Changes the permissions of the file specific by path to mode.

If path points to a symbolic link, this function will change the permissions of the target file, not the symbolic link itself, unless followSymLinks is set to false.

TODO: followSymLinks == false is not implemented and will throw.

@:value({ followSymLinks : true })staticchown (path:FilePath, uid:Int, gid:Int, followSymLinks:Bool = true):Void

Changes the owner and group of the file specific by path to uid and gid, respectively.

If path points to a symbolic link, this function will change the permissions of the target file, not the symbolic link itself, unless followSymLinks is set to false.

TODO: followSymLinks == false is not implemented and will throw.

staticcopyFile (src:FilePath, dest:FilePath):Void

Copies the file at src to dest. If dest exists, it is overwritten.

staticinlinecreateDirectory (path:String):Void

Deprecated: "use mkdir instead"

staticcreateReadStream (path:FilePath, ?options:FileReadStreamCreationOptions):FileReadStream

Creates a read stream (an instance of IReadable) for the given path. options can be used to specify how the file is opened, as well as which part of the file will be read by the stream.

  • options.flags - see open.
  • options.mode - see open.
  • options.autoClose - whether the file should be closed automatically once the stream is fully consumed.
  • options.start - starting position in bytes (inclusive).
  • options.end - end position in bytes (non-inclusive).

staticinlinedeleteDirectory (path:String):Void

Deprecated: "use rmdir instead"

staticinlinedeleteFile (path:String):Void

Deprecated: "use unlink instead"

staticexists (path:String):Bool

Returns true if the file or directory specified by path exists.

The result of this call should not be used in a condition before a call to e.g. open, because this would introduce a race condition (the file could be deleted after the exists call, but before the open call). Instead, the latter function should be called immediately and errors should be handled with a try ... catch block.

staticinlinefullPath (path:String):FilePath

Deprecated: "use realpath instead"

staticinlineisDirectory (path:String):Bool

Deprecated: "use stat(...).isDirectory() instead"

staticlink (existingPath:FilePath, newPath:FilePath):Void

@:value({ recursive : false })staticmkdir (path:FilePath, recursive:Bool = false, ?mode:FilePermissions):Void

Creates a directory at the path path, with file mode mode.

If recursive is false (default), this function can only create one directory at a time, the last component of path. If recursive is true, intermediate directories will be created as needed.

staticmkdtemp (prefix:FilePath):FilePath

Creates a unique temporary directory. prefix should be a path template ending in six X characters, which will be replaced with random characters. Returns the path to the created directory.

The generated directory needs to be manually deleted by the process.

@:value({ binary : true })staticopen (path:FilePath, ?flags:FileOpenFlags, ?mode:FilePermissions, binary:Bool = true):File

Opens the file located at path.

staticinlinereadDirectory (path:String):Array<FilePath>

Deprecated: "use readdir instead"

staticreadFile (path:FilePath, ?flags:FileOpenFlags):Bytes

Reads all the bytes of the file located at path.

staticreaddir (path:FilePath):Array<FilePath>

Reads the contents of a directory specified by path. Returns an array of FilePaths relative to the specified directory (i.e. the paths are not absolute). The array will not include . or ...

staticreaddirTypes (path:FilePath):Array<DirectoryEntry>

Same as readdir, but returns an array of DirectoryEntry values instead.

staticreadlink (path:FilePath):FilePath

Returns the contents (target path) of the symbolic link located at path.

staticrealpath (path:FilePath):FilePath

Returns the canonical path name of path (which may be a relative path) by resolving ., .., and symbolic links.

staticrename (oldPath:FilePath, newPath:FilePath):Void

Renames the file or directory located at oldPath to newPath. If a file already exists at newPath, it is overwritten. If a directory already exists at newPath, an exception is thrown.

staticrmdir (path:FilePath):Void

Deletes the directory located at path. If the directory is not empty or cannot be deleted, an error is thrown.

@:value({ followSymLinks : true })staticstat (path:FilePath, followSymLinks:Bool = true):FileStat

Available on cross

Returns information about the file located at path.

If path points to a symbolic link, this function will return information about the target file, not the symbolic link itself, unless followSymLinks is set to false.

@:value({ followSymLinks : true })staticstat (path:FilePath, followSymLinks:Bool = true):Stat

Available on eval

Returns information about the file located at path.

If path points to a symbolic link, this function will return information about the target file, not the symbolic link itself, unless followSymLinks is set to false.

@:value({ type : SymlinkType.SymlinkDir })staticsymlink (target:FilePath, path:FilePath, type:SymlinkType = SymlinkType.SymlinkDir):Void

Creates a symbolic link at path, pointing to target.

The type argument is ignored on all platforms except Windows.

@:value({ len : 0 })statictruncate (path:FilePath, len:Int = 0):Void

Truncates the file located at path to exactly len bytes. If the file was larger than len bytes, the extra data is lost. If the file was smaller than len bytes, the file is extended with null bytes.

staticunlink (path:FilePath):Void

Deletes the file located at path.

staticutimes (path:FilePath, atime:Date, mtime:Date):Void

Modifies the system timestamps of the file located at path.

@:value({ recursive : false })staticwatch (path:FilePath, recursive:Bool = false):FileWatcher

Creates a file watcher for path.

Parameters:

recursive

If true, the file watcher will signal for changes in sub-directories of path as well.

staticwriteFile (path:FilePath, data:Bytes, ?flags:FileOpenFlags, ?mode:FilePermissions):Void

Writes data to the file located at path.