Search Java Programs

Friday, February 5, 2010

Copy Directory or File in Java

Introduction
Java has the features of the copying the directory and it's contents. This example shows how to copy the directory and files into a new directory.
In this program, you will see how contains of a directory or a file is copied  from the specified source directory (subdirectories and files) to specified destination directory. If you specify the unknown source directory which does not exist then the program gives the output message like : File or directory does not exist. otherwise read the source directory name and create the OutputStream instance for the destination directory while if you specify the unknown destination directory name then the program creates new directory with that name for the destination directory where the contents of the source directory have to be copied.
In this program, there are two methods have been used to complete the program. These methods are explained ahead :
copyDirectory (File srcPath, File dstPath):

The copyDirectory() method is used to copy contents from the specified source directory to the specified destination directory. This method takes two File type arguments passed by the calling method in the main method and check whether the specified directory is a directory exactly or not. If the specified name is in the exact directory format and that exists then the method generates the list of all the sub directories and file inside the source directory using the list() method and copy one by one to the destination directory otherwise the specified name is copies (treated) as a file name directly.
And another method is the main method in which the program read the source and destination directory or file name.
Here is the code of the program : 



import java.io.*;

  public class CopyDirectory{
 
 public static void main(String[] args) throws IOException{
 
   CopyDirectory cd = new CopyDirectory();
    BufferedReader in = new BufferedReader
                        (new InputStreamReader(System.in));
    System.out.println("Enter the source directory or file name : ");

      String source = in.readLine();

    File src = new File(source);
 
    System.out.println("Enter the destination 
                 directory or file name : ");
    String destination = in.readLine();
  
      File dst = new File(destination); 

    cd.copyDirectory(src, dst);

  }
  

  public void copyDirectory(File srcPath, File dstPath)
                               throws IOException{
  
  if (srcPath.isDirectory()){

      if (!dstPath.exists()){

        dstPath.mkdir();
 
     }

 
     String files[] = srcPath.list();
  
    for(int i = 0; i < files.length; i++){
        copyDirectory(new File(srcPath, files[i]), 
                     new File(dstPath, files[i]));
 
      }

    }
 
   else{
 
      if(!srcPath.exists()){

        System.out.println("File or directory does not exist.");
 
       System.exit(0);

      }
      
else
 
      {
 
       InputStream in = new FileInputStream(srcPath);
       OutputStream out = new FileOutputStream(dstPath); 
                     // Transfer bytes from in to out
            byte[] buf = new byte[1024];
 
              int len;
 
           while ((len = in.read(buf)) > 0) {
 
          out.write(buf, 0, len);

        }
 
       in.close();
 
           out.close();

      }
 
   }
   
 System.out.println("Directory copied.");
 
 }

}
Output of the Program:
C:\nisha>java CopyDirectory
Enter the source directory or file name :test
Enter the destination directory or file name :dir1
Directory copied.
Download this example.

No comments:

Post a Comment

Website Design by Mayuri Multimedia