Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Directory listener
The file
library provides a Directory Listener
, which is used to listen to the given directory in the local file system.
It notifies when new files are created in the directory or when the existing files are deleted or modified.
For more information on the underlying module, see the file
module.
Before running this, change the value of the path
field to indicate the path of the directory that you want to monitor.
As the recursive property is set to false, the listener does not monitor the child directories of the main directory that it listens to.
import ballerina/file;
import ballerina/log;
// The listener monitors any modifications done to a specific directory.
listener file:Listener inFolder = new ({
path: "/home/ballerina/fs-server-connector/observed-dir",
recursive: false
});
// The directory listener should have at least one of these predefined resources.
service "localObserver" on inFolder {
// This function is invoked once a new file is created in the listening directory.
remote function onCreate(file:FileEvent m) {
log:printInfo("Create: " + m.name);
}
// This function is invoked once an existing file is deleted from the listening directory.
remote function onDelete(file:FileEvent m) {
log:printInfo("Delete: " + m.name);
}
// This function is invoked once an existing file is modified in the listening directory.
remote function onModify(file:FileEvent m) {
log:printInfo("Modify: " + m.name);
}
}
To run this sample, use the bal run
command. After running the sample,
create a new file called test1.txt
in the directory called observed-dir
, modify it, and delete it.
$ bal run directory_listener.baltime = 2020-12-12 13:49:08,497 level = INFO module = "" message = "Create: /home/ballerina/fs-server-connector/observed-dir/test1.txt"time = 2020-12-12 13:49:41,709 level = INFO module = "" message = "Modify: /home/ballerina/fs-server-connector/observed-dir/test1.txt"time = 2020-12-12 13:50:04,997 level = INFO module = "" message = "Delete: /home/ballerina/fs-server-connector/observed-dir/test1.txt"
PreviousTemp files and directories
NextRandom numbers