I’m getting into Camel for the first time, and working through some of the examples in Camel In Action.
These posts are my efforts at some of the examples, working on a Mac using Spring STS.
The following example is pretty much a copy and paste from the Camel In Action Book, but I’ve broken it down into simple steps.
Create the Project
Open Spring STS or Eclipse and create a new Maven Project as follows:
File -> New -> Maven Project
Check Create a Simple Project
Set the Group Id to: com.skills421.examples.camel
Set the Artifact Id to: CamelBasics

Click Finish.
Edit the Maven Dependencies
Next edit the pom.xml file as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.skills421.examples.camel</groupId> <artifactId>CamelBasics</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Core Camel --> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.5.0</version> </dependency> <!-- JUnit for testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> </project>
pom.xml
This will download camel-core-2.5.0.jar, commons-logging and commons-management for Camel and its dependencies.
It will also download junit-4.11.jar and ham crest-core for JUnit and its dependencies.
Create FileCopier.java
in the src/main folder create the following code in the com.skills421.examples.camel.basics package
package com.skills421.examples.camel.basics; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class FileCopier { public void copyFile(File source, File dest) throws IOException { OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[(int) source.length()]; FileInputStream in = new FileInputStream(source); in.read(buffer); try { out.write(buffer); } finally { out.close(); in.close(); } } }
FileCopier.java
Create The JUnit Test to test the code
Right click on FileCopier.java and select New -> JUnit Test Case

Change the source folder to: CamelBasics/src/test/java and click Finish
Now edit the FileCopierTest.java code as follows
package com.skills421.examples.camel.basics; import static org.junit.Assert.*; import java.io.File; import java.io.IOException; import org.junit.Test; public class FileCopierTest { @Test public void testCopyDirectory() { File inboxDir = new File("/Users/johndunning/Desktop/Camel/CamelIn"); File outboxDir = new File("/Users/johndunning/Desktop/Camel/CamelOut"); FileCopier fileCopier = new FileCopier(); try { outboxDir.mkdir(); File[] files = inboxDir.listFiles(); for (File source : files) { if (source.isFile()) { File dest = new File(outboxDir.getPath() + File.separator + source.getName()); fileCopier.copyFile(source, dest); } } } catch (IOException e) { fail(e.getMessage()); } } }
FileCopier.java
Note that you will need to change the inboxDir and outboxDir to a directory on your own computer system.
Create a few files in inboxDir that you expect to be copied to outboxDir
Run the JUnit Test
Right click on you FileCopierTest.java and select Run As -> JUnit Test
The code should execute and produce a green bar to show success.
If you look in the outboxDir directory that you specified, you should see that the files have been copied successfully
[…] This post continues on from Copying Files Using Java […]