How to copy a file or a folder containing files from one location to another using recursion in a Java program?
We are all aware of the most simple way of copying a single file or a complicated folder structure from one location to another in WINDOWS- just a 2 click job or Ctrl C & Ctrl V. Is there is a simple way to do in Java as well? Let's find out.....
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
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);
}
}
}
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.








Comments
Error
there is an error in code in this line in first code:
copyFile(oSourcecDir, new File(destFolder, srcFolder.getName()));
should be:
copyFile(srcFolder, new File(destFolder, srcFolder.getName()));
Post new comment