Home » Core Java » io » FileInputStream » How to copy a directory in Java

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. 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");

		} 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 = in.read(buffer)) > 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 ....



Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.