Read and Write a Flat File

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.

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 href=a flat file to JSON" />

Application XML File
  " />       " outputMimeType='application/flatfile; schemapath=$'/> (1)      /apps/$/$#["\n"]#[payload]' /> 

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

< "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 href=a flat file" />

Application XML File
  " />       "/> (1) , "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" >> ]]>   /apps/$/$#["\n"]#[payload]'/>  (2)  

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 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=#["$/apps/$/"]