Copying Files using Java

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="" xmlns:xsi=""

<!-- Core Camel -->

<!-- JUnit for testing -->




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.


in the src/main folder create the following code in the com.skills421.examples.camel.basics package

package com.skills421.examples.camel.basics;


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);;


Create The JUnit Test to test the code

Right click on and select New -> JUnit Test Case


Change the source folder to: CamelBasics/src/test/java and click Finish

Now edit the code as follows

package com.skills421.examples.camel.basics;

import static org.junit.Assert.*;


import org.junit.Test;

public class FileCopierTest

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();

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)


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 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

One thought on “Copying Files using Java

Add yours

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: