It's not about me, it's my blog!

Recursive file and folder copy in Java

Posted on: May 8, 2012

In my previous post here I have explain about the recursive file and folder copy with C#. This time I want to do the same with Java. You can easily copy files and folders by using the copy(Path, Path, CopyOption) method. However, files inside a folder are not copied and you can find the new folder empty even the destination folder has some.

In the following method I did not use such APIs, instead read byte streams from the source and copied into the destination when you want to copy files and create folders when you want copy folders from source to destination.

private void recursiveCopy(File fSource, File fDest) {
     try {
          if (fSource.isDirectory()) {
          // A simple validation, if the destination is not exist then create it
               if (!fDest.exists()) {
                    fDest.mkdirs();
               }

               // Create list of files and directories on the current source
               // Note: with the recursion 'fSource' changed accordingly
               String[] fList = fSource.list();

               for (int index = 0; index < fList.length; index++) {
                    File dest = new File(fDest, fList[index]);
                    File source = new File(fSource, fList[index]);

                    // Recursion call take place here
                    recursiveCopy(source, dest);
               }
          }
          else {
               // Found a file. Copy it into the destination, which is already created in 'if' condition above

               // Open a file for read and write (copy)
               FileInputStream fInStream = new FileInputStream(fSource);
               FileOutputStream fOutStream = new FileOutputStream(fDest);

               // Read 2K at a time from the file
               byte[] buffer = new byte[2048];
               int iBytesReads;

               // In each successful read, write back to the source
               while ((iBytesReads = fInStream.read(buffer)) >= 0) {
                    fOutStream.write(buffer, 0, iBytesReads);
               }

               // Safe exit
               if (fInStream != null) {
                    fInStream.close();
               }

               if (fOutStream != null) {
                    fOutStream.close();
               }
          }
     }
     catch (Exception ex) {
          // Please handle all the relevant exceptions here
     }
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

Categories

Calendar

May 2012
M T W T F S S
« Apr   Jun »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tweets

Blog Stats

  • 9,500 hits
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: