Java - How to copy a file or a folder containing files from one location to another using recursion in a Java program?
Java - How to copy a file or a folder containing files from one location to another using recursion in a Java program?
This article is part of a tutorial.
Tutorial Index page - Java Copy a File or Directory
Introduction
The java.io package in Java SDK provides various classes for File I/O. We are going to use some of the core classes to copy a file or a folder contaning files from one location to another. Java, an OOP- Object Oriented Programming concept is all about reusability. Therefore, for any generic calculation like our current requirement, the code you develop should be efficient and reusable for the all known circumstances.
Before designing the solution for this problem, we should see what is required of the method that will implement this functionality. It should
- Copy contents of a file from one location to another.
- Copy a folder from one location to another.
- Copy a folder that contains a complicated folder structure (sub folders)
Algorithm
Recursion- A strong and effective way to execute repetative behaviour. See below for a simple algorithm. You can use this concept of recursion for any such related problems.
- Copy Folder - (source to destination)
- If source is Folder
- List files in the source folder
- For each file in the source folder. repeat step 2.
- else If source is file
- If destination is folder
- Go to Step 4 (Give destination folder path)
- else If destination is file
- Go to Step 4
- If destination is folder
- Copy File from source to destination using File I/O InputStream & OutputStream
Implementation
Take a look the following code implemented just as instructed in the algorithm
/******************************Copy Folder***************************************/ public static void copyFolder(File srcFolder, File destFolder) throws IOException { if (srcFolder.isDirectory()) { if (! destFolder.exists()) { destFolder.mkdir(); } String[] oChildren = srcFolder.list(); for (int i=0; i < oChildren.length; i++) { copyFolder(new File(srcFolder, oChildren[i]), new File(destFolder, oChildren[i])); } } else { if(destFolder.isDirectory()) { copyFile(oSourcecDir, new File(destFolder, srcFolder.getName())); } else { copyFile(srcFolder, destFolder); } } }
/******************************Copy File***************************************/ public static void copyFile(File srcFile, File destFile) throws IOException { InputStream oInStream = new FileInputStream(srcFile); OutputStream oOutStream = new FileOutputStream(destFile); // Transfer bytes from in to out byte[] oBytes = new byte[1024]; int nLength; BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream ); while ((nLength = oBuffInputStream.read(oBytes)) > 0) { oOutStream.write(oBytes, 0, nLength); } oInStream.close(); oOutStream.close(); }
Tips
- As mentioned in the Introduction section, you can make the code as generic and reusable as possible. For instance, in addition to just copying files, you can provide the option to delete either the source file or destination file after copying or on exit of the program. (Hint: just need to add a boolean value!)
- Any File I/O functionality will throw an IOException, so remember to deal with the exception withtin the method or the method that is calling this functionality wherever appropriate
- Make these generic method static.
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
