To Write a File Using the File Connector
A Write operation is available to the File, FTP, and SFTP connectors. For these connectors, the operation writes content into the given path on demand. It supports common use cases described below.
By default, the connector writes whatever is in the message payload:
1 <file:write path="output.csv" />
However, if the payload is a different format (for example, not CSV) and you need to transform it before writing it, what do you do? If you place a Transform component before the Write operation, the message payload changes and that impacts the operation that is placed after the Write operation.
To avoid this undesired impact, you can place the transformation inside the Write operation to generate content that will be written without producing a side effect on the message in transit.
The next example uses the File connector to write the content:
1 2 3 4 5 6 7 8 <file:write path="output.csv"> <file:content>#[%dw 2.0 output application/csv --- payload.customers.email ] </file:content> </file:write>
If any of the
c directories in the example below do not exist, the Write operation will fail by default. However, if you set the
true, the connector will automatically create any missing directories.
1 <file:write path="a/b/c/myFile.txt" />
There are several file write modes you can use when writing content to a file:
OVERWRITE: If the file exists, overwrite it completely.
APPEND: If the file exists, write the content at the end of it the existing file.
CREATE_NEW: Create a new file. If the file already exists, then you will receive an error.