Home Java Example Java Io Copy Directory or File in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Copy Directory or File in Java

Advertisement
In this section, you will learn how to copy directory or file in java.

Copy Directory 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[] argsthrows 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.

                         

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: Copy Directory or File in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
leela shankar
September 22, 2011
complaint

no matter is present in the section "Copy Directory or File in Java" in java IO tutorials.
leela shankar
September 22, 2011
solution

there is already a copy of file present in the above section which would also comes under this category, so atleast make a copy of that file here.
Matt
November 5, 2011
Copy Directory or File in Java

Even though your sample code does not show up on this page (http://www.roseindia.net/java/example/java/io/CopyDirectory.shtml), it *does* down load just fine. It also compiles and executes just fine. Once again Rose has the Web's most straight-forward and unambiguous java code examples. Thanks. Matt
buddhika
January 21, 2013
codes

good
DMCA.com