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.

Share and enjoy!
© 2010-2012 Examples Java Code Geeks. Licenced under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
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.