Module : system
Module overview
This module contains functions to retrieve information about the system, the current users of the system and to perform file system based operations.
- It provides information such as environment variables, username, user home directory path, and the current working directory.
- It provides functions to perform file system based operations such as creating, deleting, renaming the file/directory and retrieving metadata of the file.
Samples
The sample given below uses the functions in the module to get the system-level information.
import ballerina/io;
import ballerina/system;
public function main() {
// Get environment variables.
io:println("Envirionment variable: " + system:getEnv("HTTP_PORT")); // E.g. “80”
// Get the user account name.
io:println("Username: " + system:getUsername()); // E.g. “john”
// Get the user home path.
io:println("User home: " + system:getUserHome()); // E.g. “/home/john”
// Get the current directory path.
io:println("Current directory: " + system:getCurrentDirectory()); // E.g. “/home/john/work”
// Check whether file or directory of the provided path exists.
boolean result = system:exists(“foo/bar.txt”);
// Create a new directory.
string|error results = system:createDir(“foo/bar”);
// Create a directory with any non existence parents.
string|error results = system:createDir(“foo/bar”, parentDirs = true);
// Remove file or directory in specified file path.
error? results = system:remove(“foo/bar.txt”);
// Remove directory in specified file path with all children.
error? results = system:remove(“foo/bar”, recursive = true);
// Rename(Move) file or directory to new path.
error? results = system:rename(“/A/B/C”, “/A/B/D”);
// Get default directory use for temporary files.
string results = system:tempDir();
// Create a file in given file path.
string|error results = system:createFile(“bar.txt”);
// Get metadata information of the file.
FileInfo|error result = system:getFileInfo(“foo/bar.txt”);
// Get list of files in the directory.
FileInfo[]|error results = system:readDir(“foo/bar”);
// Copy file ot directory to new path.
error? results = system:copy(“/A/B/C”, “/A/B/D”, replaceExisting = true);
}
FileInfo | FileInfo record contains metadata information of a file. This record is returned by getFileInfo function is os module. |
copy | Copy file/directory in old path to new path. If new path already exists, this replaces the file. |
createDir | Creates a new directory with the specified file name. If parentDirs flag is true, Creates a directory in specified path with any necessary parents. |
createFile | Creates a file in specified file path. Truncates if file already exists in the given path. |
exists | Reports whether file or directory exists for the given the path. |
getCurrentDirectory | Returns the current working directory. |
getEnv | Returns the environment variable value associated with the provided name. |
getFileInfo | Returns metadata information of the file specified in file path. |
getUserHome | Returns the current user's home directory path. |
getUsername | Returns the current user's name. |
readDir | Reads the directory and returns a list of files and directories # inside the specified directory |
remove | Removes the specified file or directory. If recursive flag is true, Removes the path and any children # it contains. |
rename | Renames(Moves) old path to new path. If new path already exists and it is not a directory, this replaces the file. |
tempDir | Returns the default directory to use for temporary files. |
uuid | Returns a random UUID string. |