import ballerina/file;
import ballerina/io;
import ballerina/time;function main (string[] args) {
file:File target = {path:"/tmp/result.txt"};
target.open(file:W);
target.close();
boolean b = target.exists();
io:println("file exists: " + b);
file:File source = {path:"/tmp/result.txt"};
file:File destination = {path:"/tmp/copy.txt"};
file:copy(source, destination);
io:println("file copied: /tmp/result.txt to /tmp/copy.txt");
destination.delete();
io:println("file deleted: /tmp/copy.txt");
destination = {path:"/tmp/move.txt"};
file:move(source, destination);
io:println("file moved: /tmp/result.txt to /tmp/move.txt"); destination.delete();
io:println("file deleted: /tmp/move.txt");
file:File dirs = {path:"/tmp/dir/abc/def"};
_ = dirs.mkdirs();
file:File possibleDir = {path:"/tmp/dir/abc"};
io:println("file is a directory: " + possibleDir.isDirectory());
file:File newFile1 = {path:"/tmp/dir/abc/file1.txt"};
_ = newFile1.createNewFile(); file:File newFile2 = {path:"/tmp/dir/abc/file2.txt"};
_ = newFile2.createNewFile(); file:File newFile3 = {path:"/tmp/dir/abc/file3.txt"};
_ = newFile3.createNewFile();
var filesList = check possibleDir.list();
int i=0;
while (i < lengthof filesList) {
io:println(filesList[i]);
i = i + 1;
}
string name = newFile1.getName();
time:Time lastModifiedTime = check newFile1.getModifiedTime();
io:println(name + " modified at: " + lastModifiedTime.time);
io:println(name + " is readable: " + newFile1.isReadable());
io:println(name + " is writable: " + newFile1.isWritable());
}
File APIThe 'ballerina.file' package contains basic file handling functions. |
|
import ballerina/file;
import ballerina/io;
import ballerina/time;
|
|
function main (string[] args) {
|
|
file:File target = {path:"/tmp/result.txt"};
target.open(file:W);
|
Create ‘File’ struct and open for writing. |
target.close();
|
Close the file once done. |
boolean b = target.exists();
io:println("file exists: " + b);
|
Check whether the file exists. |
file:File source = {path:"/tmp/result.txt"};
file:File destination = {path:"/tmp/copy.txt"};
file:copy(source, destination);
io:println("file copied: /tmp/result.txt to /tmp/copy.txt");
|
Here’s how you can copy a file. |
destination.delete();
io:println("file deleted: /tmp/copy.txt");
|
How to delete a file. |
destination = {path:"/tmp/move.txt"};
file:move(source, destination);
io:println("file moved: /tmp/result.txt to /tmp/move.txt");
|
Move source file to destination. |
destination.delete();
io:println("file deleted: /tmp/move.txt");
|
|
file:File dirs = {path:"/tmp/dir/abc/def"};
_ = dirs.mkdirs();
|
Create a directory, along with the parent directories. |
file:File possibleDir = {path:"/tmp/dir/abc"};
io:println("file is a directory: " + possibleDir.isDirectory());
|
Check if a file is a directory. |
file:File newFile1 = {path:"/tmp/dir/abc/file1.txt"};
_ = newFile1.createNewFile();
|
Create new files inside a directory (ignoring all 3 possible return values). |
file:File newFile2 = {path:"/tmp/dir/abc/file2.txt"};
_ = newFile2.createNewFile();
|
|
file:File newFile3 = {path:"/tmp/dir/abc/file3.txt"};
_ = newFile3.createNewFile();
|
|
var filesList = check possibleDir.list();
|
Get the list of files in a directory. |
int i=0;
while (i < lengthof filesList) {
io:println(filesList[i]);
i = i + 1;
}
|
Print the list of files in directory “/tmp/dir/abc”. |
string name = newFile1.getName();
time:Time lastModifiedTime = check newFile1.getModifiedTime();
io:println(name + " modified at: " + lastModifiedTime.time);
io:println(name + " is readable: " + newFile1.isReadable());
io:println(name + " is writable: " + newFile1.isWritable());
}
|
Get file meta data. |
$ ballerina run file-api.bal
file exists: true
file copied: /tmp/result.txt to /tmp/copy.txt
file deleted: /tmp/copy.txt
file moved: /tmp/result.txt to /tmp/move.txt
file deleted: /tmp/move.txt
file is a directory: true
{path:"/tmp/dir/abc/file3.txt"}
{path:"/tmp/dir/abc/file2.txt"}
{path:"/tmp/dir/abc/file1.txt"}
{path:"/tmp/dir/abc/def"}
file1.txt modified at: 1509125400000
file1.txt is readable: true
file1.txt is writable: true
|
|