Copy a multi-layer folder in drive C to drive D, with multiple layers in Drive

Source: Internet
Author: User

Copy a multi-layer folder in drive C to drive D, with multiple layers in Drive

Class CopyWenJianJia {public static void main (String [] args) throws Exception {// source folder String yuan = "C: \ nimeizide"; // destination String mudi = "D: \ nimeizide "; // create the target folder (new File (mudi )). mkdirs (); // obtain the current File or directory File [] files = (new File (yuan) in the source folder )). listFiles (); for (File file: files) {if (file. isDirectory () {String yuanDir = yuan + "\" + file. getName (); String mudiDir = mudi + "\" + file. getName (); // copy Directory copyDir (yuanDir, mudiDir);} else {copyFile (file, new File (mudi + "\" + file. getName () ;}}// copy the public static void copyDir (String yuanDir, String mudiDir) throws Exception {(new File (mudiDir) folder )). mkdirs (); File [] files = (new File (yuanDir )). listFiles (); for (File file: files) {if (file. isFile () {File yuanFile = file; // The source File mudiFile = new File (mudiDir ). getAbsolutePath () +" \ "+ File. getName (); copyFile (yuanFile, mudiFile);} else {String yuanJia = yuanDir + "\" + file. getName (); String mudiJia = mudiDir + "\" + file. getName (); copyDir (yuanJia, mudiJia) ;}}// copy the public static void copyFile (File yuanFile, File mudiFile) throws Exception {BufferedInputStream buis = new BufferedInputStream (new FileInputStream (yuanFile. getAbsoluteFile (); BufferedOutputStream Buos = new BufferedOutputStream (new FileOutputStream (mudiFile. getAbsoluteFile (); byte [] buf = new byte [1024]; int len; while (len = buis. read (buf ))! =-1) {buos. write (buf, 0, len); buos. flush ();} buis. close (); buos. close ();}}

  

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.