Skip to main content

Local Files

Local file services monitor a directory on the local file system and trigger event handlers when files are created, modified, or deleted. Use them for on-premises batch processing, development workflows, and integrations that consume files dropped into a watched directory.

Creating a local file service​

  1. Click + Add Artifact in the canvas or click + next to Entry Points in the sidebar.

  2. In the Artifacts panel, select Local Files under File Integration.

    Artifacts panel showing Local Files under File IntegrationArtifacts panel showing Local Files under File Integration
  3. In the creation form, fill in the following fields:

    Local Files creation formLocal Files creation form
    FieldDescriptionDefault
    PathDirectory path to monitor for file events (e.g., /data/incoming).Required
    RecursiveWhen set to True, monitors all subdirectories under the specified path.False

    Expand Advanced Configurations to set the listener name.

    FieldDescriptionDefault
    Listener NameIdentifier for the listener created with this service.fileListener
  4. Click Create.

  5. WSO2 Integrator opens the service in the Service Designer. The canvas shows the attached listener pill and the Event Handlers section.

    Service Designer showing the local file service canvasService Designer showing the local file service canvas
  6. Click + Add Handler to define how file events are processed.

Service and listener configuration​

In the Service Designer, click the Configure icon in the header to open the Local Files Configuration panel.

Local Files Configuration panelLocal Files Configuration panel

Select Local Files in the left panel to view service-level settings, or select fileListener under Attached Listeners to configure the listener.

Configuration for fileListener

FieldDescriptionDefault
NameIdentifier for the listener.fileListener
PathDirectory path which the listener monitors.Required
RecursiveWhen enabled, recursively monitors all subdirectories in the given directory path.False

Click + Attach Listener to attach an additional listener to the same service.

Click Save Changes to apply updates.

File handlers​

A file handler is a remote function that WSO2 Integrator calls each time a matching file system event occurs in the monitored directory.

Adding a file handler​

In the Service Designer, click + Add Handler. A Select Handler to Add panel opens on the right listing the available event types. Click an event type to open its Configure Handler panel, then click Save.

Select Handler to Add panel showing onCreate, onDelete, and onModify event typesSelect Handler to Add panel showing onCreate, onDelete, and onModify event types
HandlerTriggered when
onCreateA new file is created in the monitored directory
onDeleteA file is deleted from the monitored directory
onModifyAn existing file in the monitored directory is modified

For onCreate and onModify, the panel includes a File Handling Options section that sets what happens to the file after the handler runs:

FieldDescription
On SuccessAction to take when the handler completes without any errors: Move the file to another directory, or Delete it. See Post-processing.
On ErrorAction to take when the handler returns an error. Same choices as On Success.
Move ToDestination directory, required when Move is selected.

An onDelete handler has no file handling options, because the file is already gone when it runs.

Configure onCreate Handler panel showing File Handling Options with On Success and On Error both ticked and set to MoveConfigure onCreate Handler panel showing File Handling Options with On Success and On Error both ticked and set to Move

FileEvent​

Each handler receives a file:FileEvent parameter with details about the file system event.

FieldTypeDescription
namestringPath of the file or directory that changed (absolute when the listener was configured with an absolute path)
operationstringOne of "create", "modify", "delete" (lowercase)

Post-processing: moving or deleting files​

After an onCreate or onModify handler returns, the runtime can move the file to another directory or delete it. Configure this on the handler form, and switch to the Ballerina Code tab to review the generated annotation.

The File Handling Options section has two independent toggles:

EventTicked by default?Action pickerExtra input
On SuccessYesMove or DeleteMove To destination, required when Move is chosen
On ErrorYesMove or DeleteMove To destination, required when Move is chosen

Common combinations:

  • Move on success, move on error: archive processed files and quarantine failures in separate directories.
  • Delete on success, move on error: discard files that processed cleanly, and keep failures for review.
  • Leave the file in place: clear On Success or On Error to skip the action for that outcome.

The form does not expose preserveSubDirs. On a recursive listener, a moved file keeps its subdirectory path under the destination. To place files directly in the destination instead, set preserveSubDirs to false on the Ballerina Code tab.

The runtime applies the following rules to every action:

  • A file that already exists at the destination is not replaced. The move fails, the failure is logged, and the source file stays in place.
  • moveTo cannot be the watched directory, and on a recursive listener it cannot be a directory inside it. Attaching a service with such a destination fails, as it does when moveTo is empty or already exists as something other than a directory.
  • Only regular files are acted on. Directory and symbolic link events are skipped.
  • A failure of the action is logged and is not returned to the handler.
  • If the file is no longer present when the action runs, the action is skipped.
  • Where several services are attached to one listener, only one of them can configure an action for a given handler, and the action runs after every attached service has handled the event. The outcome of the service that configured the action decides between the success and the error action. An error returned by another attached service does not change it.
  • Moving or deleting the file produces a delete event like any other removal, so onDelete runs for it in every service that declares it.

Reading file content​

Use the ballerina/io module to read the content of the file that triggered a handler. The path of the file is available as event.name on the file:FileEvent parameter.

In the Service Designer, open the handler and build the flow with two nodes: a Call Function node that reads the file content and a Log node that prints it.

  1. Click + on the handler canvas to add a node, pick Call Function, and select fileReadString under io functions.

  2. Set event.name as an Expression in the Path field, content as the Result, and string as the Result Type. Save the node.

Add fileReadString functionAdd fileReadString function
  1. Click + after the Call Function node, pick Log → Log Info, switch the Msg field to Expression mode, and enter content. Save the node.
Handler canvas showing the Call Function and Log Info nodes wired up to read and log file contentHandler canvas showing the Call Function and Log Info nodes wired up to read and log file content

io read functions:

FunctionDescription
io:fileReadString(path)Read the file as a single UTF-8 string
io:fileReadBytes(path)Read the file as a byte array
io:fileReadLines(path)Read the file as a string[], one entry per line
io:fileReadJson(path)Read and parse the file as a json value
io:fileReadXml(path)Read and parse the file as an xml value
io:fileReadCsv(path)Read and parse CSV content as string[][] or a record array

Writing output files​

Use the ballerina/io module to write results to the local file system from within a handler.

Use a Call Function node to invoke an io write function, then add a Log node to confirm the write:

  1. Click + on the handler canvas to add a node, pick Call Function, and select fileWriteString under io functions.

  2. Set the Path to /data/outgoing/report.txt and the Content to "Processing complete.". Save the node.

Add file write string functionAdd file write string function
  1. Click + after the Call Function node, pick Log → Log Info, and enter a confirmation message such as "Output written". Save the node.
Handler canvas showing the Call Function and Log Info nodes wired up to write a file and log a confirmationHandler canvas showing the Call Function and Log Info nodes wired up to write a file and log a confirmation

io write functions:

FunctionDescription
io:fileWriteString(path, content)Write a string to a file, overwriting any existing content
io:fileWriteString(path, content, option)Write a string with io:APPEND or io:OVERWRITE option
io:fileWriteCsv(path, content)Serialize a record[] or string[][] and write it as CSV
io:fileWriteBytes(path, content)Write a byte array to a file
io:fileWriteLines(path, content)Write a string[] as lines to a file

What's next​