import ballerina/filepath;
import ballerina/io;
public function main() {
// Get the absolute representation of the path.
string absValue = checkpanic filepath:absolute("test.txt");
// Check whether the path is absolute.
boolean isAbs = checkpanic filepath:isAbsolute("/A/B/C");
io:println("/A/B/C is absolute: ", isAbs);
// Get the base name of the path.
string name = checkpanic filepath:filename("/A/B/C");
io:println("Filename of /A/B/C: ", name); // returns C
// Get the enclosing parent directory.
string parentPath = checkpanic filepath:parent("/A/B/C");
io:println("Parent of /A/B/C: ", parentPath); // returns B
// Get the shortest path name equivalent to path by purely lexical processing.
string normalizedPath = checkpanic filepath:normalize("foo/../bar");
io:println("Normalized path of foo/../bar: ", normalizedPath); // returns bar
// Get the list of path elements joined by the OS-specific Path Separator.
string[] parts = checkpanic filepath:split("/A/B/C");
io:println(io:sprintf("Path elements of /A/B/C: %s", parts)); // returns {"A", "B", "C"}
// Join any number of path elements into a single path.
string path = checkpanic filepath:build("/", "foo", "bar");
io:println("Built path of '/', 'foo', 'bar': ", path); // On Unix : returns /foo/bar
// Get the extension of the file path.
string ext = checkpanic filepath:extension("path.bal");
io:println("Extension of path.bal: ", ext); // returns bal
// Returns a relative path that is logically equivalent to the target path when joined to the base path.
string relPath = checkpanic filepath:relative("a/b/c", "a/c/d");
io:println("Relative path between 'a/b/c' and 'a/c/d': ", relPath); // On Unix : returns ../../c/d
}
File PathThe Ballerina File Path API contains utility functions to manipulate the file path in a way compatible with the target operating system. |
import ballerina/filepath;
import ballerina/io;
public function main() {
string absValue = checkpanic filepath:absolute("test.txt");
Get the absolute representation of the path.
boolean isAbs = checkpanic filepath:isAbsolute("/A/B/C");
io:println("/A/B/C is absolute: ", isAbs);
Check whether the path is absolute.
string name = checkpanic filepath:filename("/A/B/C");
io:println("Filename of /A/B/C: ", name); // returns C
Get the base name of the path.
string parentPath = checkpanic filepath:parent("/A/B/C");
io:println("Parent of /A/B/C: ", parentPath); // returns B
Get the enclosing parent directory.
string normalizedPath = checkpanic filepath:normalize("foo/../bar");
io:println("Normalized path of foo/../bar: ", normalizedPath); // returns bar
Get the shortest path name equivalent to path by purely lexical processing.
string[] parts = checkpanic filepath:split("/A/B/C");
io:println(io:sprintf("Path elements of /A/B/C: %s", parts)); // returns {"A", "B", "C"}
Get the list of path elements joined by the OS-specific Path Separator.
string path = checkpanic filepath:build("/", "foo", "bar");
io:println("Built path of '/', 'foo', 'bar': ", path); // On Unix : returns /foo/bar
Join any number of path elements into a single path.
string ext = checkpanic filepath:extension("path.bal");
io:println("Extension of path.bal: ", ext); // returns bal
Get the extension of the file path.
string relPath = checkpanic filepath:relative("a/b/c", "a/c/d");
io:println("Relative path between 'a/b/c' and 'a/c/d': ", relPath); // On Unix : returns ../../c/d
Returns a relative path that is logically equivalent to the target path when joined to the base path.
}
# To run this sample, navigate to the directory that contains the
# `.bal` file and issue the `ballerina run` command below.
ballerina run filepath.bal
On Unix:
/A/B/C is absolute: true
Filename of /A/B/C: C
Parent of /A/B/C: /A/B
Normalized path of foo/../bar: bar
Path elements of /A/B/C: A B C
Built path of '/', 'foo', 'bar': /foo/bar
Extension of path.bal: bal
Relative path between 'a/b/c' and 'a/c/d': ../../c/d
On Windows:
/A/B/C is absolute: false
Filename of /A/B/C: C
Parent of /A/B/C: \A\B
Normalized path of foo/../bar: bar
Path elements of /A/B/C: A B C
Built path of '/', 'foo', 'bar': \foo\bar
Extension of path.bal: bal
Relative path between 'a/b/c' and 'a/c/d': ..\..\c\d