Read File Content From Source Directory using File Adapter in Spring Integration


We will create an example to read file content from source directory using file adapter in Spring integration. This example will show you how to read file content from a particular location and log the file content into console. You may further process this file content for your business requirements.


Eclipse 4.12, Java 8 or 12, Gradle 5.6, Spring Boot 2.1.8

Creating Project

Create a gradle based project in Eclipse with the project’s name as spring-integration-file.

Updating Build Script

The default generated build.gradle script has to be updated to include the required dependencies.

We have two dependencies for Spring integration and file adapter in the below build script.

buildscript {
	ext {
		springBootVersion = '2.1.7.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

Creating Application Properties

We will create an file under src/main/resources which will content key/value pair properties.

For example, we will put input directory name from where file or files will be read.

The above properties file may be updated as and when required for the below examples.

Creating Config Class

We will create a config class that contains several configurations for the applications, such as, creation of Spring beans, reading properties file etc.

package com.jeejava.spring.integration.file.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.integration.config.EnableIntegration;

public class FileConfig {

	private Environment env;
	// ...


The above class will be updated as and when required for the example shown below.

Creating Main Class

Main class with @SpringBootApplication is enough to deploy the application into embedded Tomcat server.

We will run the application using CommandLineRunner.

package com.jeejava.spring.integration.file;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringIntegrationFileApp implements CommandLineRunner {

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {


The above class will be updated as and when required for the example shown below.

Reading File From Directory

We will create an example where we will read a text file from a particular directory and read the file content and log to the console.

Creating FileReadingMessageSource Bean

We need to create a FileReadingMessageSource bean in order to read the file from a source directory.

We will configure the source directory into this bean to read the file from the source directory.

Put the below code snippets into the FileConfig class.

public FileReadingMessageSource fileReadingMessageSource() {
	FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
	fileReadingMessageSource.setDirectory(new File(env.getProperty("")));
	return fileReadingMessageSource;

Reading File Content

Now we will update our main class to read the file content as a string value.

private FileReadingMessageSource fileReadingMessageSource;

public void run(String... args) throws Exception {
	File file = fileReadingMessageSource.receive().getPayload();
	String content = Files.readString(Paths.get(file.getPath()));

Testing the Application

Run the main class and you will see below output in the console:

This is a sample text file

Sample text file: Download

Source Code


Thanks for reading.

Leave a Comment