Nav

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:


         
      
1
2
3
4
<file:read path="#[path]"
  lock="true|false"
  outputEncoding="UTF-8"
  outputMimeType="application/xml" />

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_PATH error. Note that the operation does not read directories.

Mime Types and Encoding

This connector tries to determine a file’s mime type (mimeType) based on its extension. However, when first-hand knowledge of the file’s content is necessary, you can force that mimeType to a particular value by using the outputMimeType parameter.

The same process works for encoding. By default, the connector assumes that Mule runtime’s default encoding matches the one in the file. However, you can set the encoding through the outputEncoding parameter.

Why Mime Types Matter

DataWeave expressions are embeddable inside operations that generate payloads and other values. Having the correct mimeType set helps DataWeave auto-assign types and, in turn, also generate the correct outputs. It also improving the user experience by maximizing the use of DataSense’s functionality.

File Locking

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 FILE:FILE_LOCK error.

Streaming

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. That stream is repeatable by default.

For more information on this topic, see About Streaming in Mule 4.0.