Java recursion allows you to copy, paste, and delete operating system files. java Recursion

Source: Internet
Author: User

Java recursion allows you to copy, paste, and delete operating system files. java Recursion

Use Java IO recursion to copy, paste, and delete files in the operating system. Cut = copy + paste + Delete

Sample Code:

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 class FileCopyAndDelete {// delete files of this path public void del EteFile (String path) {File f = new File (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 void copyFiles (String path1, String path2) throws IOException {File f = new File (path1); if (f. isDirectory () {File file = new File (path2); I F (! 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 void copy (String path1, String path2) throws IOException {DataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream (path1); byte [] B = new byte [in. available ()]; // available returns the actual number of readable bytes, that is, the total size in. read (B); DataOutputStream out = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (path2); out. write (B); in. close (); out. close () ;}// main method public static void main (String [] args) {FileCopyAndDelete f = new FileCopyAndDelete (); // 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 ");}*/}}

 

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.