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