import ballerina/file;
import ballerina/filepath;
import ballerina/io;

public function main() {

    // Get the path of the current directory.
    io:println("Current directory: " + file:getCurrentDirectory());

    // Create a new directory.
    string|error createDirResults = file:createDir("foo");
    if (createDirResults is string) {
        io:println("Created directory path: " + createDirResults);
    }

    // Create a new directory with any none-existent parents.
    string dirPath = checkpanic filepath:build("foo", "bar");
    createDirResults = file:createDir(dirPath, true);
    if (createDirResults is string) {
        io:println("Created nested directory path: " + createDirResults);
    }

    // Create a file in the given file path.
    string|error createFileResults = file:createFile("bar.txt");
    if (createFileResults is string) {
        io:println("Created file path: " + createFileResults);
    }

    // Get metadata information of the file.
    file:FileInfo|error fileInfoResults = file:getFileInfo("bar.txt");
    if (fileInfoResults is file:FileInfo) {
        io:println("File name: " + fileInfoResults.getName());
        io:println("File size: " + fileInfoResults.getSize().toString());
        io:println("Is directory: " + fileInfoResults.isDir().toString());
        io:println("Modified at " +
                        fileInfoResults.getLastModifiedTime().toString());
    }

    // Check whether the file or directory of the provided path exists.
    boolean fileExists = file:exists("bar.txt");
    io:println("bar.txt file exists: " + fileExists.toString());

    // Copy the file or directory to the new path.
    string filePath = checkpanic filepath:build("foo", "bar", "bar.txt");
    error? copyDirResults = file:copy("bar.txt", filePath, true);
    if (copyDirResults is ()) {
        io:println("bar.txt file is copied to new path " + filePath);
    }

    // Rename(Move) the file or directory to the new path.
    string newFilePath = checkpanic filepath:build("foo", "bar1.txt");
    error? renameResults = file:rename("bar.txt", newFilePath);
    if (renameResults is ()) {
        io:println("bar.txt file is moved to new path " + newFilePath);
    }

    // Get the default directory used for temporary files.
    string tempDirPath = file:tempDir();
    io:println("Temporary directory: " + tempDirPath);

    // Get the list of files in the directory.
    file:FileInfo[]|error readDirResults = file:readDir("foo");

    // Remove the file or directory in the specified file path.
    error? removeResults = file:remove(newFilePath);
    if (removeResults is ()) {
        io:println("Remove file at " + newFilePath);
    }

    // Remove the directory in the specified file path with all its children.
    removeResults = file:remove("foo", true);
    if (removeResults is ()) {
        io:println("Remove foo directory with all child elements.");
    }
}

File

The Ballerina File API contains functions to perform file-system operations.

import ballerina/file;
import ballerina/filepath;
import ballerina/io;
public function main() {
    io:println("Current directory: " + file:getCurrentDirectory());

Get the path of the current directory.

    string|error createDirResults = file:createDir("foo");
    if (createDirResults is string) {
        io:println("Created directory path: " + createDirResults);
    }

Create a new directory.

    string dirPath = checkpanic filepath:build("foo", "bar");
    createDirResults = file:createDir(dirPath, true);
    if (createDirResults is string) {
        io:println("Created nested directory path: " + createDirResults);
    }

Create a new directory with any none-existent parents.

    string|error createFileResults = file:createFile("bar.txt");
    if (createFileResults is string) {
        io:println("Created file path: " + createFileResults);
    }

Create a file in the given file path.

    file:FileInfo|error fileInfoResults = file:getFileInfo("bar.txt");
    if (fileInfoResults is file:FileInfo) {
        io:println("File name: " + fileInfoResults.getName());
        io:println("File size: " + fileInfoResults.getSize().toString());
        io:println("Is directory: " + fileInfoResults.isDir().toString());
        io:println("Modified at " +
                        fileInfoResults.getLastModifiedTime().toString());
    }

Get metadata information of the file.

    boolean fileExists = file:exists("bar.txt");
    io:println("bar.txt file exists: " + fileExists.toString());

Check whether the file or directory of the provided path exists.

    string filePath = checkpanic filepath:build("foo", "bar", "bar.txt");
    error? copyDirResults = file:copy("bar.txt", filePath, true);
    if (copyDirResults is ()) {
        io:println("bar.txt file is copied to new path " + filePath);
    }

Copy the file or directory to the new path.

    string newFilePath = checkpanic filepath:build("foo", "bar1.txt");
    error? renameResults = file:rename("bar.txt", newFilePath);
    if (renameResults is ()) {
        io:println("bar.txt file is moved to new path " + newFilePath);
    }

Rename(Move) the file or directory to the new path.

    string tempDirPath = file:tempDir();
    io:println("Temporary directory: " + tempDirPath);

Get the default directory used for temporary files.

    file:FileInfo[]|error readDirResults = file:readDir("foo");

Get the list of files in the directory.

    error? removeResults = file:remove(newFilePath);
    if (removeResults is ()) {
        io:println("Remove file at " + newFilePath);
    }

Remove the file or directory in the specified file path.

    removeResults = file:remove("foo", true);
    if (removeResults is ()) {
        io:println("Remove foo directory with all child elements.");
    }
}

Remove the directory in the specified file path with all its children.

# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command below.
ballerina run file.bal
Current directory: /A/B
Created directory path: /A/B/foo
Created nested directory path: /A/B/foo/bar
Created file path: /A/B/bar.txt
File name: bar.txt
File size: 0
Is directory: false
Modified at time=1567597039000 zone=id=Z offset=0
bar.txt file exists: true
bar.txt file is copied to new path foo/bar/bar.txt
bar.txt file is moved to new path foo/bar1.txt
Temporary directory: /var/folders/4z/xvlnvsp54ks9793d6k5_6bw40000gn/T/
Remove file at foo/bar1.txt
Remove foo directory with all child elements.