File Connector: Reading and Writing Files in Mule
Not every integration is API-to-API. A huge share of real-world enterprise integration still runs on files — CSV exports from legacy systems, nightly batch drops, log files, generated reports. This post covers Mule's File Connector: reading files, writing files, and the trigger pattern that watches a folder for new arrivals.
Two Ways to Use the File Connector
Like the HTTP Connector, File has both trigger and action roles:
- File Listener (source) — watches a directory and triggers a flow whenever a new file appears
- File Read / Write (processors) — actively read a specific file or write data out to one
Trigger Pattern: Watching a Folder
A common real scenario: a legacy system drops a CSV export into a shared folder every night, and your integration needs to pick it up automatically.
- Drag a File Listener onto a new flow's canvas as the source
- Configure the directory to watch
- Set a matcher if you only want specific file types (e.g., only
.csvfiles)
directory: /data/incoming
matcher: *.csvWhen a matching file appears, the flow triggers automatically, and the file's contents become the payload — with attributes populated with metadata like the file name, size, and timestamp (the same Mule Event structure from earlier in this series, just with File-specific attributes this time).
Reading File Content
Once triggered, you'll usually want to parse the file content into something usable. For CSV specifically, DataWeave can read it directly by setting the right input type:
%dw 2.0
output application/json
input payload application/csv
---
payloadThis single script converts an entire CSV file into a JSON array of objects, using the CSV header row as field names — no manual parsing logic needed.
A Realistic Combined Flow
Putting it together — process a nightly product CSV export and load it into the database from the last post:
- File Listener — watches
/data/incomingfor new.csvfiles - Transform Message — parses CSV into JSON (as shown above)
- For Each scope (a component for iterating over each item in an array, running the inner steps once per item)
- Inside the For Each: Database Insert — writes each row into the products table using bind parameters
- Move/Delete the processed file — so it isn't picked up again on the next run
Writing Files
The reverse direction — generating a file from data, useful for exporting reports or creating files another system will pick up:
- Drag a Write operation onto the canvas
- Set the target path (often built dynamically, e.g. including today's date)
- Set the content — typically a DataWeave transformation producing the format you need:
%dw 2.0
output application/csv
---
payloadPath example using a dynamic filename:
%dw 2.0
output application/json
---
{
path: "/data/outgoing/export-" ++ (now() as String {format: "yyyy-MM-dd"}) ++ ".csv"
}Common Beginner Gotchas
- File locking: if another process has the file open (mid-write from the source system), your File Listener might grab it too early, resulting in a partial or corrupt read. Many File Listener configurations include settings to wait for a file to be "stable" (unchanged for a period) before triggering — worth checking if you see intermittent corrupt-file issues
- Large files and memory: reading a very large file entirely into memory as the payload can cause performance problems. Mule supports streaming for exactly this reason — processing a file in chunks rather than loading it all at once — a more advanced topic worth knowing exists once you're working with genuinely large files
- Forgetting to clean up processed files: without moving or deleting a processed file, a File Listener watching the same directory will happily process it again on next trigger (or immediately, in some polling configurations), leading to duplicate processing
Why This Connector Matters More Than It Might Seem
Beginners sometimes assume "real" integration work is all REST APIs and modern systems. In practice, file-based integration with legacy systems is extremely common in enterprise environments — many large companies still run critical processes through scheduled file drops precisely because the systems on either end were built decades apart and file exchange was the lowest common denominator that both could support. Comfort with the File Connector is a genuinely practical, frequently-used skill, not a niche one.
What's Next
We've now covered HTTP, Database, and File — three of the most common connectors. Next, we look at one of MuleSoft's most heavily used enterprise connectors: Salesforce.
Next up in this series: Salesforce Connector: A Beginner's Integration Example
visit ebook site - https://techebooks.myinstamojo.com/
0 Comments