Copy all files in one directory to another directory (Java implementation) __java

Source: Internet
Author: User
Tags file copy
First of all to talk about my thinking, to copy a directory of all the files to another directory, we do not know what the directory structure is, and do not know how many layers of the directory, the number of files, so we would like to use the loop, for! But we do not know how many layers, so the cycle can not meet our needs. People who have learned recursion know that the idea of recursion can solve this problem very well.

Recursion Here I don't say is what thing, this oneself can Baidu, Google.

Now talk about my implementation, because there may be directories or files under the directory, they are mixed together, so it is difficult for us to operate and judge, if the direct operation, we have to create a directory to locate the file. This operation is compared to replication. So what I think is, first copy the structure of the directory, and then copy the idea of the file. Here's my Code implementation,

1. First write a file copy of the operation method

2. Replication method of directory structure

3. Method of recursive source target file

4. Integration[Java] View Plain copy print? import java.io.file;   import java.io.fileinputstream;   import  java.io.filenotfoundexception;   import java.io.fileoutputstream;   import  java.io.ioexception;   import java.io.inputstream;   import java.io.OutputStream;          public class copydirectory {        public static void main (String[] args)  {            //copy of the BBS directory under D disk to the BB2/MMS1 under   d disk             copydirectory.copyfileordir ("D://bbs",  "d://bbs2//mms1");        }               /**        *  @Description:  consolidates two operations, one for all directories in the folder, and then copies all files under the directory to the corresponding directory         *  @param  srcPath    directories to replicate        *  @param  targetPath   where to copy        *  @author  L.Eric        * create: 2013-4-16       */        public static void copyfileordir (string srcpath, string  TargetPath) {           parsedir (SrcPath,targetPath);            copyallfile (Srcpath, targetpath);        }              public static  void copyallfile (String srcpath, string targetpath) {            file f = new file (Srcpath);          &nbsP; file[] filelist = f.listfiles ();            for (file f1 : filelist) {                if (F1.isfile ())  {                    copydirectory.copyfile (srcpath +  "//"  +  F1.getname (), targetpath +  "//"  + f1.getname ());                }                //to determine whether the directory                 if (F1.isdirectory ())  {                    copyallfile (F1.getpath (). toString (), targetpath +  "// " + f1.getname ());               }            }       }                /**           *   @Description:  Copy a file through a byte stream            *  @param  src       path of source file             *  @param  target    destination file path             *  @author  L.Eric           * create:  2013-4-16      */       public static  Void copyfile (string src, string target) {                inputstream is = null;                OutputStream os = null;                                try {                    is = new fileinputstream (SRC);                    os = new  FileOutputStream (target);                    byte[] buff = new byte[1024];                    int len = 0;       &NBsp;            while (Len = is.read ( buff, 0, buff.length)  != -1)  {                        os.write (Buff, 0, len) ;                   }                     System.out.println ("File copy succeeded.") ");               } catch  ( Filenotfoundexception e)  {                    // TODO Auto-generated catch block                    e.printstaCktrace ();               } catch   (ioexception e)  {                    // TODO Auto-generated catch block                    e.printstacktrace ();                } finally {                    if (Os!=null) {                        try {                            os.close ();                       }  catch  (ioexception e)  {                            // TODO  auto-generated catch block                            e.printstacktrace ();                         } finally {                            if (is!=null) {                                 try {                                     is.close ();                                 } catch  (ioexception e)  {                                     // todo auto-generated catch block                                      E.printstacktrace ();    &NBsp;                           }                            }                         }                    }               }                            }               / **       *  @Description:  Copy all directory files in one directory (copy directory structure only)         *  @param  pathName    The target directory to be replicated        *  @param   target     -generated target file directory        *  @author  l.eric        * create: 2013-4-16       */        public static void parsedir (string pathname, string  target) {           //Create a new directory             file targetfile = new file (target);            if (!targetfile.exists ())  {                targetfile.mkdirs ();            }                        //create an abstract path            file file =  new file (pathName);           if (File.isDirectory ( )) {               File[] files  = file.listfiles ();                for (file f : files) {                    if (F.isdirectory ())  {                        parsedir (F.getPath (),  target +  "//"  + f.getname ());                    }                }           }        }         } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.