Java recursive implementation of operating system files copy, paste and delete functions

Source: Internet
Author: User
Tags file copy

Java IO Recursive implementation of the operating system to copy, paste and delete files, cut = copy + paste + DELETE

code example:

Import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.FileOutputStream; Import java.io.IOException;/** * Achieve file copy and delete using recursion * * @author Foreverlover **/ Public classFilecopyanddelete {//Delete Files of this path     Public voidDeleteFile (String path) {File F=NewFile (path); if(F.isdirectory ()) {file[] File=F.listfiles ();  for(File file1:file) { This. DeleteFile (File1.tostring ());            File1.delete (); }        } Else{f.delete ();    } f.delete (); }    //copy files from path1 to path2     Public voidCopyFiles (String path1, String path2) throws IOException {File F=NewFile (path1); if(F.isdirectory ()) {File File=NewFile (path2); if(!file.exists ()) File.mkdir (); File[] File1=F.listfiles ();  for(File file2:file1) {CopyFiles (file2.tostring (), path2+"/"+file2.getname ()); }        } Else{copy (path1, path2); }    }    //copy file from path1 to path2 one by one     Public voidCopy (String path1, String path2) throws IOException {DataInputStreaminch=NewDataInputStream (NewBufferedinputstream (NewFileInputStream (path1)); byte[] B =New byte[inch. available ()];//available returns the actual number of readable bytes, that is, the total size        inch. Read (b); DataOutputStream out=NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream (path2));  out. Write (b); inch. Close ();  out. Close (); }    //Main Method     Public Static voidMain (string[] args) {Filecopyanddelete F=NewFilecopyanddelete (); //test copy files using recursive        /** {String path1 = "D://folder1"; String path2 = "D://folder2"; try {* F.copyfiles (path1, path2); System.out.println ("Ok,copy FINISH");         } * catch (IOException e) {e.printstacktrace ();}} */        //Test Delete files using recursive        /** {f.deletefile ("c://folder1"); * SYSTEM.OUT.PRINTLN ("Ok,delete FINISH"); }         */    }}

Java recursive implementation of operating system files copy, paste and delete functions

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.