Jan 212017

ADM

How to copy directory in Java

  • 21 January 2017
  • ADM

 

How to copy directory in Java - images/logos/java.jpg

 

Here is an example how to copy a directory and all its sub-directories and files to a new destination directory.

Example

package com.admfactory.io;

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 CopyDirectory {

    /***
     * Copy file method using classic approach by using streams.
     * 
     * @param source
     *            file
     * @param dest
     *            file
     * @throws IOException
     */
    private static void copyFile(File source, File dest) throws IOException {
	InputStream input = null;
	OutputStream output = null;
	try {
	    input = new FileInputStream(source);
	    output = new FileOutputStream(dest);
	    byte[] buf = new byte[1024];
	    int bytesRead;
	    while ((bytesRead = input.read(buf)) > 0) {
		output.write(buf, 0, bytesRead);
	    }
	    System.out.println("File copied from " + source + "  to " + dest);
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    if (input != null)
		input.close();
	    if (output != null)
		output.close();
	}
    }

    private static void copyFolder(File source, File dest) throws IOException {
	if (source.isDirectory()) {
	    if (!dest.exists()) {
		dest.mkdirs();
		System.out.println("Directory copied from " + source + "  to " + dest);
	    }

	    String files[] = source.list();

	    for (String file : files) {
		/**
		 * construct the src and dest file structure by adding the
		 * current folder
		 */
		File srcFile = new File(source, file);
		File destFile = new File(dest, file);
		/** recursive copy the files or sub-folders */
		copyFolder(srcFile, destFile);
	    }
	} else {
	    copyFile(source, dest);
	}
    }

    public static void main(String[] args) throws Exception {

	System.out.println("Copy Directory Example");
	System.out.println();

	File dirSrc = new File("D:\\admfactory.com\\dir1");
	File dirDest = new File("D:\\admfactory.com\\dir2");

	copyFolder(dirSrc, dirDest);

    }
}

To copy files I used the classic approach of using streams, but you can use also any methods described on the How to copy files in Java tutorial.

Output

Copy Directory Example

Directory copied from D:\admfactory.com\dir1\dir3  to D:\admfactory.com\dir2\dir3
Directory copied from D:\admfactory.com\dir1\dir3\dir4  to D:\admfactory.com\dir2\dir3\dir4
File copied from D:\admfactory.com\dir1\dir3\dir4\f6.txt  to D:\admfactory.com\dir2\dir3\dir4\f6.txt
File copied from D:\admfactory.com\dir1\dir3\dir4\f7.txt  to D:\admfactory.com\dir2\dir3\dir4\f7.txt
File copied from D:\admfactory.com\dir1\dir3\f4.txt  to D:\admfactory.com\dir2\dir3\f4.txt
File copied from D:\admfactory.com\dir1\dir3\f5.txt  to D:\admfactory.com\dir2\dir3\f5.txt
File copied from D:\admfactory.com\dir1\f1.txt  to D:\admfactory.com\dir2\f1.txt
File copied from D:\admfactory.com\dir1\f2.txt  to D:\admfactory.com\dir2\f2.txt
File copied from D:\admfactory.com\dir1\f3.txt  to D:\admfactory.com\dir2\f3.txt

 

References