Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

How to copy a directory in Java

In this tutorial we are going to see how you can copy a directory in Java. The idea is very simple. You open the directory and you list the contents. Then you open the contents and examine them one by one. If the entry you are looking at is a File, you can copy it to the new location. If the entry is a directory, you open the directory and follow the same actions recursively.
Ok, that was the basic idea let’s see the code:



public class CopyDirectoryJavaExample {
	public static void main(String[] args) {

		File srcFolderPath = new File("C:\\Users\\nikos7\\Desktop\\files");
		File destFolderPath = new File("C:\\Users\\nikos7\\Desktop\\newFiles");

		if (!srcFolderPath.exists()) {

			System.out.println("Directory does not exist. Will exit now");

		} else {

			try {
				copyFolder(srcFolderPath, destFolderPath);
			} catch (IOException e) {


		System.out.println("Directory coping from " + srcFolderPath + "  to "
				+ destFolderPath + " was finished successfully");

	public static void copyFolder(File srcFolderPath, File destFolderPath)
			throws IOException {

		if (!srcFolderPath.isDirectory()) {

			// If it is a File the Just copy It to the new Folder
			InputStream in = new FileInputStream(srcFolderPath);
			OutputStream out = new FileOutputStream(destFolderPath);

			byte[] buffer = new byte[1024];

			int length;

			while ((length = > 0) {
				out.write(buffer, 0, length);

			System.out.println("File copied from " + srcFolderPath + " to "
					+ destFolderPath + " successfully");

		} else {

			// if it is a directory create the directory inside the new destination directory and
			// list the contents...
			if (!destFolderPath.exists()) {
				System.out.println("Directory copied from " + srcFolderPath
						+ "  to " + destFolderPath + " successfully");

			String folder_contents[] = srcFolderPath.list();

			for (String file : folder_contents) {

				File srcFile = new File(srcFolderPath, file);
				File destFile = new File(destFolderPath, file);

				copyFolder(srcFile, destFile);



File copied from C:\Users\nikos7\Desktop\files\internalFolder\test3.txt to C:\Users\nikos7\Desktop\newFiles\internalFolder\test3.txt successfully
File copied from C:\Users\nikos7\Desktop\files\internalFolder\test4.txt to C:\Users\nikos7\Desktop\newFiles\internalFolder\test4.txt successfully
File copied from C:\Users\nikos7\Desktop\files\internalFolder\test5.txt to C:\Users\nikos7\Desktop\newFiles\internalFolder\test5.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test.gzip to C:\Users\nikos7\Desktop\newFiles\test.gzip successfully
File copied from C:\Users\nikos7\Desktop\files\test.txt to C:\Users\nikos7\Desktop\newFiles\test.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test1.txt to C:\Users\nikos7\Desktop\newFiles\test1.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test2.txt to C:\Users\nikos7\Desktop\newFiles\test2.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test3.txt to C:\Users\nikos7\Desktop\newFiles\test3.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test4.txt to C:\Users\nikos7\Desktop\newFiles\test4.txt successfully
File copied from C:\Users\nikos7\Desktop\files\test5.txt to C:\Users\nikos7\Desktop\newFiles\test5.txt successfully
Directory coping from C:\Users\nikos7\Desktop\files  to C:\Users\nikos7\Desktop\newFiles was finished successfully

This was an example on how to copy a directory in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.