File to String Transformer in Mule
The file-to-string-transformer element configures a transformer that reads the contents of a java.io.File into a java.lang.String.
This transformer does not close file streams. This prevents files from being deleted or moved if the flow is asynchronous. If you have streaming enabled for an asynchronous endpoint, use the ObjectToString transformer instead.
File to String transformer reads the content of the file and converts into string form.
flow of File to String Transformer :
first logger is printing payload in object form and second logger is printing payload in readable string form because of File to String Transformer.
Logger output :
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<file:connector name="File" outputPattern="file.txt" readFromDirectory="src/main/resources/input" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="test_filetostringFlow">
<file:inbound-endpoint path="src/main/resources/input" connector-ref="File" pollingFrequency="10000" responseTimeout="10000" doc:name="File"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<file:file-to-string-transformer doc:name="File to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>