To Read a File Using the File Connector
The File connector can read a file at any point in the flow, unlike in Mule 3, where the transport can only read files as a result of inbound endpoint polling.
Here is the syntax for reading a file:
<file:read path="#[path]" lock="true|false" outputEncoding="UTF-8" outputMimeType="application/csv; headers=false" />
The operation above reads the file in the given path. It returns a
MuleMessage with the following attributes:
The file’s content as payload.
The file’s metadata in the message attributes.
If the file does not exist, you receive a
FILE:ILLEGAL_PATHerror. Note that the operation does not read directories.
Each connector tries to determine a file’s MIME type (
mimeType) from its extension. However, if first-hand knowledge of the file’s content is necessary, you can force the MIME type to a different value with the
The same process works for encoding. By default, the connector assumes that the default encoding in the Mule runtime matches the MIME type of the file. However, you can set the encoding through the
DataWeave expressions are embeddable inside operations that generate payloads and other values. Having the correct
mimeType set helps DataWeave auto-assign types and also generate the correct outputs. Also, maximizing the use of DataSense’s functionality improves the user experience.
You can set a file system lock on the file when it is being read by setting the
lock parameter to
true (defaults to
false). When enabled, this feature asks the operating system to lock the file, which prevents any other process (or Mule flow) from accessing that file while the lock is held. The lock is automatically released when one of the following things occurs:
The Mule flow, which locked the file, ends.
The file content has been fully read.
Note that if the file is already locked by someone else, the connector will not be able to lock it, and you will receive a
The List operation makes use of the repeatable streams functionality introduced in Mule 4. The operation returns a list of messages, where each message represents a file in the list and holds a stream to the file. A stream is repeatable by default.
For more information on this topic, see Streaming in Mule 4.