Java--> using recursive methods to copy directories, subdirectories, and files (files used in IO streams)

Source: Internet
Author: User

 PackageCom.dragon.java.newcopyfile;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classTest { Public Static voidMain (String args[]) {File file=NewFile ("F:/exercises"); File file2=NewFile ("F:/book");    CopyFile (file, file2); }        //Recursive method     Public Static voidcopyFile (file file, file file2) {//When a directory is found, create a directory        if(File.isdirectory ()) {File2.mkdir (); file[] Files=File.listfiles ();  for(File file3:files) {//RecursiveCopyFile (File3,NewFile (File2, File3.getname ())); }                //When a file is found}Else if(File.isfile ()) {file File3=NewFile (File2.getabsolutepath ()); Try{file3.createnewfile ();            Copydatas (File.getabsolutepath (), File3.getabsolutepath ()); } Catch(IOException e) {e.printstacktrace (); }        }    }        //ways to copy file data     Public Static voidCopydatas (String filePath, String filePath1) {FileInputStream fis=NULL; FileOutputStream Fos=NULL; Try {                        //Byte streamFIS =NewFileInputStream (FilePath); FOS=NewFileOutputStream (FILEPATH1); byte[] buffer =New byte[1024];  while(true) {                inttemp = fis.read (buffer, 0, buffer.length); if(temp = =-1) {                     Break; } Else{fos.write (buffer,0, temp); }            }        } Catch(IOException e) {System.out.println (e); } finally {            Try{fis.close ();            Fos.close (); } Catch(IOException e) {System.out.println (e); }        }    }}    

Java--> using recursive methods to copy directories, subdirectories, and files (files used in IO streams)

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.