Class

class Directory

public class Directory <: Iterable<FileInfo> {
    public init(path: Path)
    public init(path: String)
}

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.

Parent Type:

prop info

public prop info: FileInfo

Description: Obtains the metadata of the current directory.

Type: FileInfo

init(Path)

public init(path: Path)

Description: Creates a Directory instance.

Parameters:

  • path: Path: directory path in the Path format

Throws:

  • FSException: If the path is invalid, this exception is thrown.

init(String)

public init(path: String)

Description: Creates a Directory instance that can be an absolute path or a relative path. If the path is invalid, an exception is thrown.

Parameters:

  • path: String: directory path in the string format

Throws:

  • FSException: If the path does not exist or is empty, this exception is thrown.

static func copy(Path, Path, Bool)

public static func copy(sourceDirPath: Path, destinationDirPath: Path, overwrite: Bool): Unit

Description: Copies the directory, files, and subfolders to a new location.

Parameters:

  • sourceDirPath: Path: source directory path in the Path format
  • destinationDirPath: Path: destination directory path in the Path format
  • overwrite: Bool: whether to overwrite: true: yes, false: no

Throws:

  • FSException: If the source directory does not exist, the destination directory exists when overwrite is set to false, the destination directory is in the source directory, or the copy fails, this exception is thrown.
  • IllegalArgumentException: If the source or destination directory name contains null characters, this exception is thrown.

static func copy(String, String, Bool)

public static func copy(sourceDirPath: String, destinationDirPath: String, overwrite: Bool): Unit

Description: Copies the directory, files, and subfolders to a new location. If the copy fails, an exception is thrown.

Parameters:

  • sourceDirPath: String: source directory path in the string format
  • destinationDirPath: String: destination directory path in the string format
  • overwrite: Bool: whether to overwrite: true: yes, false: no

Throws:

  • FSException: If the source directory does not exist, the destination directory exists when overwrite is set to false, the destination directory is in the source directory, or the copy fails, this exception is thrown.
  • IllegalArgumentException: If the source or destination directory name contains null characters, this exception is thrown.

static func create(Path, Bool)

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

Description: Creates a directory.

You can specify whether to create directories recursively. If yes, 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

Returns:

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): Directory

Description: Creates a directory.

You can specify whether to create directories recursively. If yes, 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

Returns:

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): Directory

Description: Creates a temporary directory in a specified directory.

Parameters:

  • directoryPath: Path: directory path in the Path format

Returns:

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): Directory

Description: Creates a temporary directory in a specified directory.

Parameters:

  • directoryPath: String: directory path in the string format

Returns:

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 delete(Path, Bool)

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

Description: Deletes a directory.

You can specify whether to delete directories recursively. If yes, directories are deleted level by level.

Parameters:

  • path: Path: specified directory path in the Path format
  • recursive!: Bool: whether to delete directories recursively: true: yes, false (default): no

Throws:

  • FSException: If the directory does not exist or the recursive deletion fails, this exception is thrown.
  • IllegalArgumentException: If path is an empty string, contains null characters, or contains more than 4096 characters, this exception is thrown.

static func delete(String, Bool)

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

Description: Deletes a directory.

You can specify whether to delete directories recursively. If yes, directories are deleted level by level.

Parameters:

  • path: String: specified directory path in the string format
  • recursive!: Bool: whether to delete directories recursively: true: yes, false (default): no

Throws:

  • FSException: If the directory does not exist or the recursive deletion fails, this exception is thrown.
  • IllegalArgumentException: If path is an empty string, contains null characters, or contains more than 4096 characters, this exception is thrown.

static func exists(Path)

public static func exists(path: Path): Bool

Description: Checks whether the directory corresponding to a path exists.

Parameters:

  • path: Path: directory path in the Path format

Returns:

  • Bool: If the path does not exist or does not correspond to a directory, false is returned. If the path corresponds to a directory or a soft link that points to a directory, true is returned.

static func exists(String)

public static func exists(path: String): Bool

Description: Checks whether the directory corresponding to a path exists.

Parameters:

  • path: String: directory path in the string format

Returns:

  • Bool: If the path does not exist or does not correspond to a directory, false is returned. If the path corresponds to a directory or a soft link that points to a directory, true is returned.

static func move(Path, Path, Bool)

public static func move(sourceDirPath: Path, destinationDirPath: Path, overwrite: Bool): Unit

Description: Moves the directory, files, and subfolders to a specified path.

Parameters:

  • sourceDirPath: Path: source directory path in the Path format
  • destinationDirPath: Path: destination directory path in the Path format
  • overwrite: Bool: whether to overwrite: true: all subfolders and files in the destination path are overwritten, false: no

Throws:

  • IllegalArgumentException: If the destination directory name is empty or contains null characters, this exception is thrown.
  • FSException: If the source directory does not exist, the destination directory already exists when overwrite is set to false, or the moving fails, this exception is thrown.

static func move(String, String, Bool)

public static func move(sourceDirPath: String, destinationDirPath: String, overwrite: Bool): Unit

Description: Moves the directory, files, and subfolders to a specified path.

Parameters:

  • sourceDirPath: String: source directory path in the string format
  • destinationDirPath: String: destination directory path in the string format
  • overwrite: Bool: whether to overwrite: true: all subfolders and files in the destination path are overwritten, false: no

Throws:

  • IllegalArgumentException: If the destination directory name is empty or contains null characters, this exception is thrown.
  • FSException: If the source directory does not exist, the destination directory already exists when overwrite is set to false, or the moving fails, this exception is thrown.

func createFile(String)

public func createFile(name: String): File

Description: Creates a subfile with a specified name in the current Directory.

Parameters:

  • name: String: subfile name. The parameter must be a string without a path prefix.

Returns:

  • File: File instance of the subfile, which must be closed by manually calling the close function.

Throws:

  • FSException: If the subfile name contains path information or the file name already exists, this exception is thrown.
  • IllegalArgumentException: If the file name contains null characters, this exception is thrown.

func createSubDirectory(String)

public func createSubDirectory(name: String): Directory

Description: Creates a subdirectory with a specified name in the current Directory.

Parameters:

  • name: String: subdirectory name. The parameter must be a string without a path prefix.

Returns:

Throws:

  • FSException: If the subdirectory name contains path information, the path already exists, or the directory fails to be created, this exception is thrown.
  • IllegalArgumentException: If the directory name contains null characters, this exception is thrown.

func directories()

public func directories(): Iterator<FileInfo>

Description: Returns the subdirectory iterator of the current directory.

Returns:

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

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

func directoryList()

public func directoryList(): ArrayList<FileInfo>

Description: Returns the subdirectory list of the current directory.

Returns:

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

func entryList()

public func entryList(): ArrayList<FileInfo>

Description: Returns the file or subdirectory list of the current directory.

Returns:

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

func fileList()

public func fileList(): ArrayList<FileInfo>

Description: Returns the subfile list of the current directory.

Returns:

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

func files()

public func files(): Iterator<FileInfo>

Description: Returns the subfile iterator of the current directory.

Returns:

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

func isEmpty()

public func isEmpty(): Bool

Description: Checks whether the current directory is empty.

Returns:

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

Throws:

  • FSException: If an error occurs in the system interface called at the bottom layer, this exception is thrown.

func iterator()

public func iterator(): Iterator<FileInfo>

Description: Returns the file or subdirectory iterator of the current directory.

Returns:

  • Iterator<FileInfo>: the file or subdirectory iterator of the current directory

Throws:

  • FSException: If the member information of the directory fails to be obtained, this exception is thrown.

class File

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

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 will open the corresponding file by default. After the file is used, you need to call the close function to close the file in a timely manner. Otherwise, resource leakage occurs.

Parent Type:

prop fileDescriptor

public prop fileDescriptor: FileDescriptor

Description: Obtains the file descriptor information.

Type: FileDescriptor

prop info

public prop info: FileInfo

Description: Obtains the file metadata.

Type: FileInfo

prop length

public prop length: Int64

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

Type: Int64

prop position

public prop position: Int64

Description: Obtains the current cursor position of a file.

Type: Int64

prop remainLength

public prop remainLength: Int64

Description: Obtains the number of bytes of a file from the current cursor position to the file trailer.

Type: Int64

init(Path, OpenOption)

public init(path: Path, openOption: OpenOption)

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 path or an absolute path.

Parameters:

Throws:

  • FSException: If the file already exists when the creation is performed, the file does not exist when other operations are performed, the parent directory of the file does not exist, or the file cannot be opened due to other reasons, this exception is thrown.
  • IllegalArgumentException: If path is empty or contains null characters, this exception is thrown.

init(String, OpenOption)

public init(path: String, openOption: OpenOption)

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 path or an absolute path.

Parameters:

Throws:

  • FSException: If the file already exists when the creation is performed, the file does not exist when other operations are performed, the parent directory of the file does not exist, or the file cannot 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 copy(Path, Path, Bool)

public static func copy(sourcePath: Path, destinationPath: Path, overwrite: Bool): Unit

Description: Copies a file to a new location. If the copy fails, an exception is thrown.

Parameters:

  • sourcePath: Path: source path of the file
  • destinationPath: Path: destination path of the file
  • overwrite: Bool: whether to overwrite: true: yes, false: no

Throws:

  • FSException: If the file path is empty, the read permission on the source path file is not granted, or the destination path file exists but the write permission is not granted, this exception is thrown.
  • IllegalArgumentException: If the file path contains null characters, this exception is thrown.

static func copy(String, String, Bool)

public static func copy(sourcePath: String, destinationPath: String, overwrite: Bool): Unit

Description: Copies a file to a new location. If the copy fails, an exception is thrown.

Parameters:

  • sourcePath: String: source path of the file
  • destinationPath: String: destination path of the file
  • overwrite: Bool: whether to overwrite: true: yes, false: no

Throws:

  • FSException: If the file path is empty, the read permission on the source path file is not granted, or the destination path file exists but the write permission is not granted, this exception is thrown.
  • IllegalArgumentException: If the file path 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 upper-level 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 upper-level 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 delete(Path)

public static func delete(path: Path): Unit

Description: Deletes a file in a specified path.

Before deleting a file, ensure that the file exists and is closed. Otherwise, the deletion may fail.

Parameters:

  • path: Path: file path

Throws:

  • FSException: If the specified file does not exist or fails to be deleted, this exception is thrown.

static func delete(String)

public static func delete(path: String): Unit

Description: Deletes a file in a specified path.

Before deleting a file, ensure that the file exists and is closed. Otherwise, the deletion may fail.

Parameters:

  • path: String: file path string

Throws:

  • FSException: If the specified file does not exist or fails to be deleted, this exception is thrown.

static func exists(Path)

public static func exists(path: Path): Bool

Description: Checks whether the file corresponding to a path exists.

Parameters:

  • path: Path: file path

Returns:

  • Bool: If the path does not exist or the path does not correspond to a file, false is returned. If the path corresponds to a file or a soft link to a file, true is returned.

static func exists(String)

public static func exists(path: String): Bool

Description: Checks whether the file corresponding to a path exists.

Parameters:

  • path: String: file path string

Returns:

  • Bool: If the path does not exist or the path does not correspond to a file, false is returned. If the path corresponds to a file or a soft link to a file, true is returned.

static func move(Path, Path, Bool)

public static func move(sourcePath: Path, destinationPath: Path, overwrite: Bool): Unit

Description: Moves a file.

If overwrite is set to true and a file with the same name exists in the specified path, the existing file is overwritten. If overwrite is set to false, the existing file is not overwritten. If the destination directory already exists, an exception is thrown.

Parameters:

  • sourcePath: Path: source path of the file
  • destinationPath: Path: destination path of the file
  • overwrite: Bool: whether to overwrite: true: yes, false: no

Throws:

  • IllegalArgumentException: If the destination directory name is empty or contains null characters, this exception is thrown.
  • FSException: If the source directory does not exist, the destination directory already exists when overwrite is set to false, or the moving fails, this exception is thrown.

static func move(String, String, Bool)

public static func move(sourcePath: String, destinationPath: String, overwrite: Bool): Unit

Description: Moves a file.

If overwrite is set to true and a file with the same name exists in the specified path, the existing file is overwritten. If overwrite is set to false, the existing file is not overwritten. If the destination directory already exists, an exception is thrown.

Parameters:

  • sourcePath: String: source path of the file
  • destinationPath: String: destination path of the file
  • overwrite: Bool: whether to overwrite: true: the file in the destination path is overwritten, false: no

Throws:

  • IllegalArgumentException: If the destination directory name is empty or contains null characters, this exception is thrown.
  • FSException: If the source directory does not exist, the destination directory already exists when overwrite is set to false, or the moving fails, this exception is thrown.

static func openRead(Path)

public static func openRead(path: Path): File

Description: Opens a file in a specified path in read-only mode.

Parameters:

  • path: Path: file path

Returns:

  • File: File instance, which must be closed by manually calling the close function.

Throws:

  • FSException: If the file does not exist or is unreadable, this exception is thrown.
  • IllegalArgumentException: If the file path contains null characters, this exception is thrown.

static func openRead(String)

public static func openRead(path: String): File

Description: Opens a file in a specified path in read-only mode.

Parameters:

  • path: String: file path string

Returns:

  • File: File instance, which must be closed by manually calling the close function.

Throws:

  • FSException: If the file does not exist or is unreadable, this exception is thrown.
  • IllegalArgumentException: If the file path 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>, OpenOption)

public static func writeTo(path: Path, buffer: Array<Byte>, openOption!: OpenOption = CreateOrAppend): Unit

Description: Opens a file in a specified path by using openOption and writes the buffer.

Parameters:

  • path: Path: file path
  • buffer: Array<Byte>: bytes to be written
  • openOption: OpenOption: file opening option. The default value is CreateOrAppend.

Throws:

static func writeTo(String, Array<Byte>, OpenOption)

public static func writeTo(path: String, buffer: Array<Byte>, openOption!: OpenOption = CreateOrAppend): Unit

Description: Opens a file in a specified path by using openOption and writes the buffer.

Parameters:

  • path: String: file path string
  • buffer: Array<Byte>: bytes to be written
  • openOption: OpenOption: file opening option. The default value is CreateOrAppend.

Throws:

  • FSException: If the file path is empty, 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 openOption used to create the file object. After the file object is closed, false is returned.

Returns:

  • Bool: If the object is readable, true is returned. If the object is unreadable or the file object is 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 openOption used to create the file object. After the file object is closed, false is returned.

Returns:

  • Bool: If the object is writable, true is returned. If the object is not writable or the file object is 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 copyTo(OutputStream)

public func copyTo(out: OutputStream): Unit

Description: Writes all data that has not been read by File to a specified OutputStream.

Parameters:

Throws:

  • FSException: If the file is not opened or the read permission on the file is not granted, 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 to the 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 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 readToEnd()

public func readToEnd(): Array<Byte>

Description: Reads all remaining data of the current File and returns the remaining bytes in Array<Byte> format.

Returns:

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

Throws:

  • FSException: If the data fails to be read 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 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 to the maximum file size minus 4096 bytes.

Parameters:

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

Returns:

  • Int64: the 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 write(Array<Byte>)

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

Description: Writes the data in the buffer to a file.

Parameters:

  • buffer: Array<Byte>: buffer to which data is to be written. If 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 not writable, this exception is thrown.