Apache Camel – Convert File to String

Hello everyone, In this post, you will learn How to Convert File to String using Apache Camel. The example has been tested and shared in the post.

Maven Dependencies

<dependency>
	<groupid>org.apache.camel</groupid>
	<artifactid>camel-core</artifactid>
	<version>${camel-version}</version>
</dependency>

Example Program

package com.dineshkrish.example6;

import java.io.File;
import java.nio.file.Files;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class FileToString {

	public static void main(String[] args) throws Exception {

		// creating the camel context
		CamelContext context = new DefaultCamelContext();

		try {
			context.addRoutes(new RouteBuilder() {

				@Override
				public void configure() throws Exception {

					from("file:input_dir")
					.process(new Processor() {
						public void process(Exchange exchange) throws Exception {

							// get the file
							File file = exchange.getIn().getBody(File.class);

							// extract the content of the file
							byte[] bytes = Files.readAllBytes(file.toPath());
							String content = new String(bytes);

							// set the content
							exchange.getOut().setBody(content);
						}
					})
					.to("seda:end");
				}
			});

			// start the context
			context.start();

			// creating the consumer template
			ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
			String content = consumerTemplate.receiveBody("seda:end", String.class);

			System.out.println(content);

			// stop the context
			context.stop();

		} catch (Exception e) {
			context.stop();
			e.printStackTrace();
		}

	}
}

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hello everyone, This is Dinesh Krishnan