Java Replication folder and all subdirectories and files

Source: Internet
Author: User

 Packagetext;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintStream; Public classfileCopy { Public Static voidMain (string[] args)throwsioexception {copy ("G:/test", "E:/test"); System.out.println ("Copy succeeded!"); }      Private Static voidCopy (String f1, string F2)throwsIOException {File file1=NewFile (F1); /*file File2=new file (F2);*/file[] Flist=File1.listfiles ();  for(File f:flist) {if(F.isfile ()) {CopyFile2 (F.getpath ()), F2+ "/" +f.getname ());//How to call a copy fileSystem.out.println ("Original Path [" +f.getpath () + "] copied path [" +f2+ "/" +f.getname () + "]"); }Else if(F.isdirectory ()) {CopyFile1 (F.getpath ()), F2+ "/" +f.getname ());//How to call a copy folderSystem.out.println ("Original Path [" +f.getpath () + "] copied path [" +f2+ "/" +f.getname () + "]"); }         }         }      /*** Copy Folder *@throwsIOException*/    Public Static voidCopyFile1 (String f1,string F2)throwsioexception{//Create a folderFile file=NewFile (F2); if(!file.exists ())        {File.mkdirs ();   } copy (F1,F2); }        /*** Copy files *@throwsIOException*/      Public Static voidCopyFile2 (String f1, string F2)throwsIOException {Try{InputStream input=NewFileInputStream (F1); OutputStream Output=NewFileOutputStream (F2); byte[] bt=New byte[1024]; if((input!=NULL) && (output!=NULL)){                 while((Input.read (BT))! = (-1) {output.write (BT,0, bt.length);               }} input.close ();                      Output.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); }   }}

Java Replication folder and all subdirectories and files

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.