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:

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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");
			System.exit(0);

		} else {

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

		}

		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 = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}

			in.close();
			out.close();
			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()) {
				destFolderPath.mkdir();
				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);
			}

		}
	}
}

Output:

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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 two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close