Classes

class Directory

public class Directory {}

Description: Provides capabilities such as creating, moving, copying, deleting, querying, and traversing a directory in a file system.

NOTE

An invalid path is one of the following:

  • A path containing invalid characters, such as spaces, tabs, and newline characters
  • A path containing invalid characters, such as special characters and control characters
  • A path containing a directory or file that does not exist
  • A path containing a directory or file that cannot be accessed due to insufficient permissions or lock status

Enter a valid path without invalid characters so that the target file or directory can be accessed correctly.

static func create(Path, Bool)

public static func create(path: Path, recursive!: Bool = false): Unit

Description: Creates a directory.

You can specify whether to create directories recursively. If so, create directories that do not exist in the path level by level.

Parameters:

  • path: Path: path of the directory to be created
  • recursive!: Bool: whether to create directories recursively: true: yes; false (default): no

Throws:

  • FSException: If the directory already exists or if the intermediate directory does not exist in non-recursive creation mode, this exception is thrown.
  • IllegalArgumentException: If the directory is empty, the directory is the current directory, the directory is the root directory, or the directory contains null characters, this exception is thrown.

static func create(String, Bool)

public static func create(path: String, recursive!: Bool = false): Unit

Description: Creates a directory.

You can specify whether to create directories recursively. If so, create directories that do not exist in the path level by level.

Parameters:

  • path: String: path of the directory to be created
  • recursive!: Bool: whether to create directories recursively: true: yes; false (default): no

Throws:

  • FSException: If the directory already exists or if the intermediate directory does not exist in non-recursive creation mode, this exception is thrown.
  • IllegalArgumentException: If the directory is empty, the directory is the current directory, the directory is the root directory, or the directory contains null characters, this exception is thrown.

static func createTemp(Path)

public static func createTemp(directoryPath: Path): Path

Description: Creates a temporary directory in a specified directory.

Parameters:

  • directoryPath: Path: directory path in path format

Returns:

  • Path: path of the temporary directory

Throws:

  • FSException: If the directory does not exist or fails to be created, this exception is thrown.
  • IllegalArgumentException: If the directory is empty or contains null characters, this exception is thrown.

static func createTemp(String)

public static func createTemp(directoryPath: String): Path

Description: Creates a temporary directory in a specified directory.

Parameters:

  • directoryPath: String: directory path in string format

Returns:

  • Path: path of the temporary directory

Throws:

  • FSException: If the directory does not exist or fails to be created, this exception is thrown.
  • IllegalArgumentException: If the directory is empty or contains null characters, this exception is thrown.

static func isEmpty(Path)

public static func isEmpty(path: Path): Bool

Description: Checks whether a specified directory is empty.

Parameters:

  • path: Path: path of the directory to be checked

Returns:

  • Bool: If the directory is empty, true is returned. Otherwise, false is returned.

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or an error occurs in the system API called at the bottom layer during the check, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

static func isEmpty(String)

public static func isEmpty(path: String): Bool

Description: Checks whether a specified directory is empty.

Parameters:

  • path: String: path of the directory to be checked

Returns:

  • Bool: If the directory is empty, true is returned. Otherwise, false is returned.

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or an error occurs in the system API called at the bottom layer during the check, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

static func readFrom(Path)

public static func readFrom(path: Path): Array<FileInfo>

Description: Obtains a list of subitems in the current directory.

The sequence of subitems in an array depends on that of files in the system.

Parameters:

  • path: Path: path of the directory whose subitems are to be read

Returns:

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or the member information of the directory fails to be obtained, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

static func readFrom(String)

public static func readFrom(path: String): Array<FileInfo>

Description: Obtains a list of subitems in the current directory.

The sequence of subitems in an array depends on that of files in the system.

Parameters:

  • path: String: path of the directory whose subitems are to be read

Returns:

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or the member information of the directory fails to be obtained, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

static func walk(Path, (FileInfo)->Bool)

public static func walk(path: Path, f: (FileInfo)->Bool): Unit

Description: Traverses subitems (non-recursive, meaning that the subitems do not contain subdirectories) in the directory corresponding to path and executes a callback function for each subitem.

The walk function exits when the traversal is complete or the callback function f returns false. The traversal sequence depends on the sequence of files in the system.

Parameters:

  • path: Path: path of the directory to be traversed
  • f: (FileInfo) -> Bool: callback function executed for each subitem. The input parameter is metadata information of the subitem, and the return value indicates whether to continue the traversal.

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or the member information of the directory fails to be obtained, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

static func walk(String, (FileInfo)->Bool)

public static func walk(path: String, f: (FileInfo)->Bool): Unit

Description: Traverses subitems (non-recursive, meaning that the subitems do not contain subdirectories) in the directory corresponding to path and executes a callback function for each subitem.

The walk function exits when the traversal is complete or the callback function f returns false. The traversal sequence depends on the sequence of files in the system.

Parameters:

  • path: String: path of the directory to be traversed
  • f: (FileInfo) -> Bool: callback function executed for each subitem. The input parameter is metadata information of the subitem, and the return value indicates whether to continue the traversal.

Throws:

  • FSException: If the specified path does not exist, the specified path is not a directory, or the member information of the directory fails to be obtained, this exception is thrown.
  • IllegalArgumentException: If the specified path is empty or contains null characters, this exception is thrown.

class File

public class File <: Resource & IOStream & Seekable {
    public init(path: String, mode: OpenMode)
    public init(path: Path, mode: OpenMode)
}

Description: Provides functions for performing operations on files, including opening, creating, closing, moving, copying, deleting, and querying files, and reading and writing files using a stream.

NOTE

An invalid path is one of the following:

  • A path containing invalid characters, such as spaces, tabs, and newline characters
  • A path containing invalid characters, such as special characters and control characters
  • A path containing a directory or file that does not exist
  • A path containing a directory or file that cannot be accessed due to insufficient permissions or lock status

Enter a valid path without invalid characters so that the target file or directory can be accessed correctly.

NOTE

  • The created file object opens the corresponding file by default. After using the object, you need to call the close function to close the file in a timely manner to prevent resource leakage.

Parent types:

prop fileDescriptor

public prop fileDescriptor: FileDescriptor

Description: Obtains file descriptor information.

Type: FileDescriptor

prop info

public prop info: FileInfo

Description: Obtains file metadata information.

Type: FileInfo

prop length

public prop length: Int64

Description: Obtains the number of bytes from the file header to the file trailer.

Type: Int64

init(Path, OpenMode)

public init(path: Path, mode: OpenMode)

Description: Creates a file object.

The file path and file opening mode (read and write permissions) must be specified, and the path can be a relative or absolute path.

Parameters:

Throws:

  • FSException: If the file is opened in read-only mode but the file does not exist, the parent directory of the file does not exist, or the file fails to be opened due to other reasons, this exception is thrown.
  • IllegalArgumentException: If the path is empty or contains null characters, this exception is thrown.

init(String, OpenMode)

public init(path: String, mode: OpenMode)

Description: Creates a file object.

The file path and file opening mode (read and write permissions) must be specified, and the path can be a relative or absolute path.

Parameters:

Throws:

  • FSException: If the file is opened in read-only mode but the file does not exist, the parent directory of the file does not exist, or the file fails to be opened due to other reasons, this exception is thrown.
  • IllegalArgumentException: If the path is an empty string or contains null characters, this exception is thrown.

static func appendTo(Path, Array<Byte>)

public static func appendTo(path: Path, buffer: Array<Byte>): Unit

Description: Opens a file in a specified path and writes buffer in append mode. If the file does not exist, it will be created.

Parameters:

Throws:

  • FSException: If the file fails to be opened or written, this exception is thrown.
  • IllegalArgumentException: If the file path is empty or contains null characters, this exception is thrown.

static func appendTo(String, Array<Byte>)

public static func appendTo(path: String, buffer: Array<Byte>): Unit

Description: Opens a file in a specified path and writes buffer in append mode. If the file does not exist, it will be created.

Parameters:

Throws:

  • FSException: If the file fails to be opened or written, this exception is thrown.
  • IllegalArgumentException: If the file path is empty or contains null characters, this exception is thrown.

static func create(Path)

public static func create(path: Path): File

Description: Creates a file in a specified path in write-only mode.

Parameters:

  • path: Path: file path

Returns:

Throws:

  • FSException: If the parent directory of the file to which the path points does not exist or the file already exists, this exception is thrown.
  • IllegalArgumentException: If the file path is empty or contains null characters, this exception is thrown.

static func create(String)

public static func create(path: String): File

Description: Creates a file in a specified path in write-only mode.

Parameters:

  • path: String: file path string

Returns:

Throws:

  • FSException: If the parent directory of the file to which the path points does not exist or the file already exists, this exception is thrown.
  • IllegalArgumentException: If the file path is an empty string or contains null characters, this exception is thrown.

static func createTemp(Path)

public static func createTemp(directoryPath: Path): File

Description: Creates a temporary file in a specified directory.

The name of the created file is tmpFileXXXXXX. The temporary files that are not used must be deleted manually.

Parameters:

  • directoryPath: Path: directory path

Returns:

  • File: file instance of the temporary file

Throws:

  • FSException: If the file fails to be created or the path does not exist, this exception is thrown.
  • IllegalArgumentException: If the file path is empty or contains null characters, this exception is thrown.

static func createTemp(String)

public static func createTemp(directoryPath: String): File

Description: Creates a temporary file in a specified directory.

The name of the created file is tmpFileXXXXXX. The temporary files that are not used must be deleted manually.

Parameters:

  • directoryPath: String: directory path string

Returns:

  • File: file instance of the temporary file

Throws:

  • FSException: If the file fails to be created or the path does not exist, this exception is thrown.
  • IllegalArgumentException: If the file path is an empty string or contains null characters, this exception is thrown.

static func readFrom(Path)

public static func readFrom(path: Path): Array<Byte>

Description: Reads all content of a file in a specified path and returns the content in the form of a byte array.

Parameters:

  • path: Path: file path

Returns:

  • Array<Byte>: all content of the file in the form of a byte array

Throws:

  • FSException: If the file path is empty, the file is unreadable, or the file fails to be read, this exception is thrown.
  • IllegalArgumentException: If the file path contains null characters, this exception is thrown.

static func readFrom(String)

public static func readFrom(path: String): Array<Byte>

Description: Reads all content of a file in a specified path and returns the content in the form of a byte array.

Parameters:

  • path: String: file path string

Returns:

  • Array<Byte>: all content of the file in the form of a byte array

Throws:

  • FSException: If the file fails to be read or closed, the file path is empty, or the file is unreadable, this exception is thrown.
  • IllegalArgumentException: If the file path contains null characters, this exception is thrown.

static func writeTo(Path, Array<Byte>)

public static func writeTo(path: Path, buffer: Array<Byte>): Unit

Description: Opens a file in a specified path and writes buffer in overwrite mode. If the file exists, it is truncated to 0 bytes. If the file does not exist, it will be created.

Parameters:

Throws:

  • FSException: If the file fails to be opened or written, this exception is thrown.
  • IllegalArgumentException: If the file path is empty or contains null characters, this exception is thrown.

static func writeTo(String, Array<Byte>)

public static func writeTo(path: String, buffer: Array<Byte>): Unit

Description: Opens a file in a specified path and writes buffer in overwrite mode. If the file exists, it is truncated to 0 bytes. If the file does not exist, it will be created.

Parameters:

Throws:

  • FSException: If the file fails to be opened or written, this exception is thrown.
  • IllegalArgumentException: If the file path is an empty string or contains null characters, this exception is thrown.

func canRead()

public func canRead(): Bool

Description: Checks whether the current file object is readable.

The return value of this function is determined by openMode used to create the file object. After the file object is closed, false is returned.

Returns:

  • Bool: If the file object is readable, true is returned. If the file object is unreadable or closed, false is returned.

func canWrite()

public func canWrite(): Bool

Description: Checks whether the current file object is writable.

The return value of this function is determined by openMode used to create the file object. After the file object is closed, false is returned.

Returns:

  • Bool: If the file object is writable, true is returned. If the file object is unwritable or closed, false is returned.

func close()

public func close(): Unit

Description: Closes the current file object.

Throws:

  • FSException: If the object fails to be closed, this exception is thrown.

func flush()

public func flush(): Unit

Description: Writes buffer data to a stream. Because File does not have a buffer, this function does not work.

func isClosed()

public func isClosed(): Bool

Description: Checks whether the current file object is closed.

Returns:

  • Bool: If the object is closed, true is returned. Otherwise, false is returned.

func read(Array<Byte>)

public func read(buffer: Array<Byte>): Int64

Description: Reads data from a file and stores the data in a buffer.

Parameters:

  • buffer: Array<Byte>: buffer storing the data to be read

Returns:

  • Int64: If the data is read successfully, the number of read bytes is returned. If all data in the file is read, 0 is returned.

Throws:

  • IllegalArgumentException: If the buffer is empty, this exception is thrown.
  • FSException: If the data fails to be read, the file is closed, or the file is unreadable, this exception is thrown.

func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

Description: Moves the cursor to a specified position.

The specified position cannot be located before the file header but can exceed the file trailer. However, the maximum offset from the specified position to the file header cannot exceed the maximum value allowed by the current file system. The maximum value is close to the maximum file size allowed by the current file system and generally equals the maximum file size minus 4,096 bytes.

Parameters:

  • sp: SeekPosition: specified position to which the cursor is moved

Returns:

  • Int64: offset (in bytes) from the file header to the position to which the cursor is moved

Throws:

  • FSException: If the specified position does not meet the preceding requirements or the file cannot be sought, this exception is thrown.

func setLength(Int64)

public func setLength(length: Int64): Unit

Description: Truncates the current file to a specified length. If the value of length is greater than the current file length, 0 is used to pad the file until the target length is reached. This method does not change the position of the file cursor.

Parameters:

  • length: Int64: specified length to be truncated to

Throws:

func write(Array<Byte>)

public func write(buffer: Array<Byte>): Unit

Description: Writes data in a buffer to a file.

Parameters:

  • buffer: Array<Byte>: buffer to which data is to be written. If the buffer is empty, no operation is performed.

Throws:

  • FSException: If the data fails to be written, only part of data is written, the file is closed, or the file is unwritable, this exception is thrown.
public class HardLink

Description: Provides APIs for processing hard links of the file system.

static func create(Path, Path)

public static func create(link: Path, to!: Path): Unit

Description: Creates a hard link to an existing path. If the new path already exists, it will not be overwritten.

Parameters:

  • link: Path: name of the new path
  • to!: Path: name of the existing path

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the hard link fails to be created, this exception is thrown.

static func create(String, String)

public static func create(link: String, to!: String): Unit

Description: Creates a hard link to an existing path. If the new path already exists, it will not be overwritten.

Parameters:

  • link: String: name of the new path
  • to!: String: name of the existing path

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the hard link fails to be created, this exception is thrown.
public class SymbolicLink

Description: Provides APIs for processing symbolic links of the file system.

static func create(Path, Path)

public static func create(link: Path, to!: Path): Unit

Description: Creates a symbolic link to an existing path.

NOTE

On Windows, if a symbolic link whose target path does not exist is created, a file symbolic link is created. If the target path is created as a directory later, the symbolic link does not take effect.

Parameters:

  • link: Path: symbolic link to be created
  • to!: Path: target path of the symbolic link to be created

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the symbolic link fails to be created, this exception is thrown.

static func create(String, String)

public static func create(link: String, to!: String): Unit

Description: Creates a symbolic link to an existing path.

NOTE

On Windows, if a symbolic link whose target path does not exist is created, a file symbolic link is created. If the target path is created as a directory later, the symbolic link does not take effect.

Parameters:

  • link: String: symbolic link to be created
  • to!: String: target path of the symbolic link to be created

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the symbolic link fails to be created, this exception is thrown.

static func readFrom(Path, Bool)

public static func readFrom(path: Path, recursive!: Bool = false): Path

Description: Obtains a target address of a specified symbolic link. If recursive is set to true, the link that points to the final target address is traced, and the full path of the target address is returned. If recursive is set to false, the link that points to the current target address is read and returned.

Parameters:

  • path: Path: address of the symbolic link
  • recursive!: Bool: whether to recursively read the target addresses. The default value is false.

Returns:

  • Path: target address of the symbolic link

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the symbolic link fails to be read, this exception is thrown.

static func readFrom(String, Bool)

public static func readFrom(path: String, recursive!: Bool = false): Path

Description: Obtains a target address of a specified symbolic link. If recursive is set to true, the link that points to the final target address is traced, and the full path of the target address is returned. If recursive is set to false, the link that points to the current target address is read and returned.

Parameters:

  • path: String: address of the symbolic link
  • recursive!: Bool: whether to recursively read the target addresses. The default value is false.

Returns:

  • Path: target address of the symbolic link

Throws:

  • IllegalArgumentException: If the path in the parameter is empty or contains null characters, this exception is thrown.
  • FSException: If the symbolic link fails to be read, this exception is thrown.