Over a million developers have joined DZone.

Connecting to Files, Part 1

DZone 's Guide to

Connecting to Files, Part 1

In this article, I will be talking about the outbound file endpoint and showing how you can write a file to a particular location using Anypoint Studio.

· Integration Zone ·
Free Resource

Using the File Connector in Mule, applications have the ability to read or write the files in the local system. The following operations can be performed while using Mule's file connector:

  • Poll a file or pattern of files (ex:- .*csv) from a particular directory every certain period of time and delete, move, or leave the file as it is processed.
  • Copy files from one directory to another.
  • Read input files while saving a backup of the input file.
  • Create new files with a specific name.
  • Append output to existing files.

You can use the file connector as a:

1. Inbound Endpoint.

  • Add the File endpoint to the source section of a flow.
  • It checks for files in a location at some set frequency.
  • It triggers the flow whenever it receives an incoming file.
  • It reads the file into the payload and dispatches the message to the next processor.
  • By default, it consumes the file, but it can also move and/or rename the file.
  • It can filter the files by different name patterns as it reads through them.

Image title

2. Outbound Endpoint.

  • Add the File endpoint to the process section of a flow.
  • It will serve as an outbound endpoint, passing files to the connected file system.
  • It can generate new files or append content to an existing file.
  • The file name can be set at runtime.

Image title

In this article, I will be talking about the outbound file endpoint and I will show how you can write a file to a particular location using Anypoint Studio.

Write a File

We will try to write a file in a particular directory with a filename passed into a query string at runtime through Postman and with some default file content.

Take an HTTP connector and drop it in Designer. Then drag and drop the other elements as well, as shown in below design. 

Image title

Create a Global element named HTTP Listener Configuration with the below settings:

Image title

Then configure the path in HTTP as shown here:

Image title

Set the FlowVariable with the below value:

Image title

Set the payload value as per your requirements (i.e. whatever you would like to write into a file). In my case, I am writing the payload as “First Demo on how to write a file.” All you really need to do is give just a string value to showcase.

Now I am trying to read the filename dynamically, create a file with that filename, write the contents in the Set Payload, and then add a logger just for debugging purposes.

Image title

Now run this application and hit the request from Postman with the below URL :


Image title

You will notice that the response status is 200, and the payload is returned as a response. If you go to the directory ‘Dzone’ and search for file ‘Pankaj.txt’ in my case, you will be able to see that.

mule ,integration ,endpoints

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}