FileInputStreamFileOutputStream

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.

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button