Module : file
Module overview
This module contains functions to perform file system based operations such as create, delete, rename the file/directory and retrieve metadata of the file.
Sample
The sample given below uses the functions in the module to get the system-level information.
import ballerina/file;
import ballerina/io;
public function main() {
// Get the current directory path.
io:println("Current directory: " + file:getCurrentDirectory());
// Check whether file or directory of the provided path exists.
boolean result = file:exists("foo/bar.txt");
// Create a new directory.
string | error results = file:createDir("foo/bar");
// Create a directory with any non-existent parents.
string | error results = file:createDir("foo/bar", true);
// Remove the file or directory in the specified file path.
error? results = file:remove("foo/bar.txt");
// Remove the directory in the specified file path with all its children.
error? results = file:remove("foo/bar", true);
// Rename(Move) the file or directory to the new path.
error? results = file:rename("/A/B/C", "/A/B/D");
// Get default directory use for temporary files.
string results = file:tempDir();
// Create a file in given file path.
string | error results = file:createFile("bar.txt");
// Get metadata information of the file.
file:FileInfo | error result = file:getFileInfo("foo/bar.txt");
// Get the list of files in the directory.
file:FileInfo[] | error results = file:readDir("foo/bar");
// Copy the file or directory to the new path.
error? results = file:copy("/A/B/C", "/A/B/D", true);
}
Detail | Record type to hold the details of an error. |
FileEvent | Represents an event which will trigger when there is a changes to listining direcotry. |
ListenerConfig | Represents configurations that required for directory listener. |
FileInfo | FileInfo record contains metadata information of a file. This record is returned by getFileInfo function is os module. |
Listener | Represents directory listener endpoint where used to listen to a directory in the local file system. |
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. |
getFileInfo | Returns metadata information of the file specified in file path. |
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. |
INVALID_OPERATION_ERROR | Identifies invalid operation error. |
PERMISSION_ERROR | Identifies permission error. |
FILE_SYSTEM_ERROR | Identifies file system error. |
FILE_NOT_FOUND_ERROR | Identifies file not found error. |
Error | Represents file system related errors. |
FileNotFoundError | Represents an error that occurs when the file/directory does not exist at the given filepath. |
FileSystemError | Represents an error that occurs when a file system operation fails. |
InvalidOperationError | Represents an error that occurs when a file system operation is denied due to invalidity. |
PermissionError | Represents an error that occurs when a file system operation is denied, due to the absence of file permission. |