---
schema_version: "1"
id: 9cdf099244a4c33d6491b48e7b26200e7902d2aa5bf5150bd2afc5e4b136aef5
title: Read and Write a Flat File
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook-flat-file-read-and-write.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-flat-file-read-and-write
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 6d345bd8091bdff39713b5e686f14193ff6d9bdc3b033b03cb9bb4d7a90e3f5f
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, xml, json, dataweave, data weave, datamapper, dwl, dfl, dw, flat file
---

# Read and Write a Flat File

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The following examples show you how to read the contents of a flat file, and also how to write a
flat file following the specified schema. Each example consists of a Mule application that uses
DataWeave to read and write flat files.

When you work with flat files, configure the flat file schema to use for reading and writing flat
file content. The flat file schema defines the structure of a flat file, and this enables Mule to
write and read in this format.

These example applications are configured to work with the sample flat file data and the example
flat file schema provided in [Flat File Schemas](https://docs.mulesoft.com/dataweave/2.3/dataweave-flat-file-schemas#full_schema_ex).

## Example of Reading a Flat File

The following example application consists of:

1.  A Scheduler component that triggers the flow

2.  A File Read operation that reads a flat file from the specified path

3.  A Transform Message component that transforms the content read from the flat file to JSON format

4.  A Logger component that outputs the current payload

![A flow showing the process of reading and transforming a flat file to JSON](/mulesoft/current/en-us/dataweave/2.3/_images/mruntime-read-flat-file.png)

Application XML File

```xml
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
  xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
  xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
  <file:config name="File_Config" doc:name="File Config" >
    <file:connection workingDir="${fileWorkingDir}" />
  </file:config>
  <configuration-properties doc:name="Configuration properties" file="flatfileconfig.properties" />
  <flow name="ReadFlatfileFlow" >
    <scheduler doc:name="Scheduler">
      <scheduling-strategy>
        <fixed-frequency frequency="10000" />
      </scheduling-strategy>
    </scheduler>
    <file:read doc:name="Read flat file" config-ref="File_Config"
    path="${fileReadPath}"
    outputMimeType='application/flatfile; schemapath=${schemaPath}'/> (1)
    <ee:transform doc:name="Transform to JSON"  >
      <ee:message >
        <ee:set-payload ><![CDATA[%dw 2.0
        output application/json
        ---
        payload]]>
        </ee:set-payload>
      </ee:message>
    </ee:transform>
    <logger level="INFO" doc:name="Logger" message='#["\n"]Reading flat file from: ${mule.home}/apps/${app.name}/${fileReadPath}#["\n"]#[payload]' />
  </flow>
</mule>
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>Note that the File Read operation configures the following attributes:<div><ul><li><p><code>path</code>: The path of the flat file to read</p></li><li><p><code>outputMimeType</code>: The output MIME type of the content, in this case, <code>application/flatfile</code></p></li><li><p><code>schemapath</code>: The reader property that specifies the path to the flat file schema (<code>.ffd</code> file)</p></li></ul></div></td></tr></tbody></table>

After its execution, the example application generates the following output:

```json
{
  "Batch": [
    {
      "TDR": [
        {
          "Record Type": "BAT",
          "Sequence Number": 2,
          "Amount": 32876,
          "Account Number": "0123456789",
          "Batch Function": "D",
          "Type": "CR"
        },
        {
          "Record Type": "BAT",
          "Sequence Number": 3,
          "Amount": 87326,
          "Account Number": "0123456788",
          "Batch Function": "D",
          "Type": "CR"
        }
      ],
      "BCF": {
        "Record Type": "BAT",
        "Sequence Number": 4,
        "Batch Transaction Count": 2,
        "Batch Transaction Amount": 120202,
        "Unique Batch Identifier": "A000000001",
        "Batch Function": "T",
        "Type": "CR"
      },
      "BCH": {
        "Record Type": "BAT",
        "Company Name": "ACME RESEARCH",
        "Sequence Number": 1,
        "Unique Batch Identifier": "A000000001",
        "Batch Function": "H"
      }
    },
    {
      "TDR": [
        {
          "Record Type": "BAT",
          "Sequence Number": 6,
          "Amount": 3582,
          "Account Number": "1234567890",
          "Batch Function": "D",
          "Type": "DB"
        },
        {
          "Record Type": "BAT",
          "Sequence Number": 7,
          "Amount": 256,
          "Account Number": "1234567891",
          "Batch Function": "D",
          "Type": "CR"
        }
      ],
      "BCF": {
        "Record Type": "BAT",
        "Sequence Number": 8,
        "Batch Transaction Count": 2,
        "Batch Transaction Amount": 3326,
        "Unique Batch Identifier": "A000000002",
        "Batch Function": "T",
        "Type": "DB"
      },
      "BCH": {
        "Record Type": "BAT",
        "Company Name": "AJAX EXPLOSIVES",
        "Sequence Number": 5,
        "Unique Batch Identifier": "A000000002",
        "Batch Function": "H"
      }
    }
  ],
  "RQF": {
    "Record Type": "RQF",
    "File Batch Count": 2,
    "File Transaction Count": 8,
    "File Transaction Amount": 116876,
    "Unique File Identifier": "A000000001",
    "Type": "CR"
  },
  "RQH": {
    "Record Type": "RQH",
    "File Creation Time": "10:10:00",
    "File Creation Date": "2018-09-01",
    "Currency": "USD",
    "Unique File Identifier": "A000000001"
  }
}
```

## Example of Writing a Flat File

The following example application contains:

1.  A Scheduler component that triggers the flow

2.  A File Read operation that reads a file containing the JSON output from the previous example

3.  A Transform Message component that transforms the JSON payload to the specified flat file format

4.  A Logger component that outputs the current payload

5.  A File Write operation that outputs the payload to a flat file in the specified target path

![A flow showing the process of writing a flat file](/mulesoft/current/en-us/dataweave/2.3/_images/mruntime-write-flat-file.png)

Application XML File

```xml
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
  xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
  xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
  <file:config name="File_Config" doc:name="File Config" >
    <file:connection workingDir="${fileWorkingDir}" />
  </file:config>
  <configuration-properties doc:name="Configuration properties" file="flatfileconfig.properties" />
  <flow name="WriteFlatFile" >
    <scheduler doc:name="Scheduler" >
    	<scheduling-strategy >
        <fixed-frequency frequency="10000" />
    	</scheduling-strategy>
    </scheduler>
    <file:read doc:name="Read JSON file" config-ref="File_Config" path="${jsonFileReadPath}"/>
    <ee:transform doc:name="Transform to flat file"> (1)
    	<ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
output application/flatfile schemaPath = "flatfileschema.ffd"
%dw 2.0
---
{
	RQH: {
		"Record Type": payload.RQH."Record Type",
		"File Creation Date": payload.RQH."File Creation Date" as Date {format: "yyyy-MM-dd"},
		"File Creation Time": payload.RQH."File Creation Time" as Time,
		"Unique File Identifier": payload.RQH."Unique File Identifier",
		Currency: payload.RQH.Currency
	},
	Batch: payload.Batch map ( batch , indexOfBatch ) -> {
		BCH: {
			"Record Type": batch.BCH."Record Type",
			"Sequence Number": batch.BCH."Sequence Number",
			"Batch Function": batch.BCH."Batch Function",
			"Company Name": batch.BCH."Company Name",
			"Unique Batch Identifier": batch.BCH."Unique Batch Identifier"},
		TDR: batch.TDR map (tdr, indexOfTdr) ->
			{
			"Record Type": tdr.TDR."Record Type",
			"Sequence Number": tdr.TDR."Sequence Number",
			"Batch Function": tdr.TDR."Batch Function",
			"Account Number": tdr.TDR."Account Number",
			"Amount": tdr.TDR.Amount,
			"Type": tdr.TDR.'Type'
			},
		BCF: {
			"Record Type": batch.BCF."Record Type",
			"Sequence Number": batch.BCF."Sequence Number",
			"Batch Function": batch.BCF."Batch Function",
			"Batch Transaction Amount": batch.BCF."Batch Transaction Amount",
			"Type": batch.BCF.'Type',
			"Batch Transaction Count": batch.BCF."Batch Transaction Count",
			"Unique Batch Identifier": batch.BCF."Unique Batch Identifier"}
		},
	RQF: {
		"Record Type": payload.RQF."Record Type",
		"File Batch Count": payload.RQF."File Batch Count",
		"File Transaction Count": payload.RQF."File Transaction Count",
		"File Transaction Amount": payload.RQF."File Transaction Amount",
		Type: payload.RQF.Type,
		"Unique File Identifier": payload.RQF."Unique File Identifier"
	}
}
]]>
      </ee:set-payload>
    	</ee:message>
    </ee:transform>
    <logger level="INFO" doc:name="Logger" message='#["\n"]Writing flat file to: ${mule.home}/apps/${app.name}/${fileWritePath}#["\n"]#[payload]'/>
    <file:write doc:name="Write flat file" config-ref="File_Config" path="${fileWritePath}" /> (2)
	</flow>
</mule>
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>Note that the Transform component specifies the following writer properties in the script header:<div><ul><li><p><code>output</code>: The output MIME type of the transformation, in this case, <code>application/flatfile</code></p></li><li><p><code>schemapath</code>: The writer property that specifies the path to the flat file schema (<code>.ffd</code> file)</p></li></ul></div></td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>Note that the File Write operation specifies the <code>path</code> property to define where to write the flat file</td></tr></tbody></table>

After its execution, the example application generates the following output:

RQH201809011010A000000001USD BAT000001HACME RESEARCH A000000001 BAT000000D 0000000000 BAT000000D
0000000000 BAT000004T0000120202CR000002A000000001 BAT000005HAJAX EXPLOSIVES A000000002 BAT000000D
0000000000 BAT000000D 0000000000 BAT000008T0000003326DB000002A000000002
RQF0002000008000000116876CRA000000001

## Test the Examples Locally

If you want to test the examples locally, follow these steps to create and configure your example
Mule application:

1.  Create a new **Mule Project** in Anypoint Studio.

2.  Copy the XML code of the application you want to test into the **Configuration XML** view of
    your project.

3.  Use [Add Modules](https://docs.mulesoft.com/studio/add-modules-in-studio-to) to add the Anypoint Connector for File
    (File Connector) to your project.

4.  Create a file named `flatfileconfig.properties` in the `src/main/resources` folder of your Mule
    application, and set its content with the following properties:

    fileReadPath=flatfile schemaPath=flatfileschema.ffd fileWritePath=flatfilenew
    jsonFileReadPath=flatFileJson.json fileWorkingDir=#\["${mule.home}/apps/${app.name}/"\]

5.  Create a file named `flatfile` in the \`sr:page-aliases:

6.  Create a file named `flatfileschema.ffd` in the \`sr:page-aliases:

7.  Create a file named `flatFileJson.json` in the `src/main/resources` folder of your Mule
    application and set its content with the
    [JSON transformation output](#json-transformation-output) from the example that reads a flat
    file.

## See Also

- [Flat File Format](https://docs.mulesoft.com/dataweave/2.3/dataweave-formats-flatfile)

- [Flat File Schemas](https://docs.mulesoft.com/dataweave/2.3/dataweave-flat-file-schemas)

- [Set Reader and Writer Properties](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-set-reader-writer-props)

- [Configure Properties](https://docs.mulesoft.com/mule-runtime/4.3/configuring-properties)
