Module : file
Module overview
This module contains functions to perform file system based operations.
- 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/file;
public function main() {
// Get the current directory path.
io:println("Current directory: " + file:getCurrentDirectory()); // E.g. “/home/john/work”
// 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 none-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);
}
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. |
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 | |
PERMISSION_ERROR | |
FILE_SYSTEM_ERROR | |
FILE_NOT_FOUND_ERROR |
Error |
FileNotFoundError | |
FileSystemError | |
InvalidOperationError | |
PermissionError |