Copy folders using Io streams (the folder contains subdirectories)

Source: Internet
Author: User

When we want to copy folders with subdirectories, we must use recursion to copy all the folders in place.

Ideas and steps: There are two scenarios for folder replication.

(1) When we traverse a file, if the file in the target folder is a standard file, we can directly copy the file,

(2) When the target file contains a folder, recursive folder is required until it is a standard file, and we are doing a copy operation.

With the above two cases, we need to provide two methods for this requirement. One is to copy standard files, and the other is to copy files with folders.

The first is the standard byte replication action, which is easy to implement.

For the second method, because there may still be folders under the folder, recursion is required at that time. When copying folders, if it is a standard file, we can directly copy it by using the throttling copy action. If it still carries a directory, we can use recursion to call our own method, we know that the last part is the standard file. At this time, we will replicate the byte in the outbound stack.

The Code is as follows:

Package wjd. copy; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; public class copyfile {public static void main (string [] ARGs) throws ioexception {// location of the folder to be copied string url1 = "C:" + file. separator + "Source"; // string url2 = "D:" + file. separator + "copy "; // Wrap these two folders into the file object file file1 = new file (url1); file file2 = new file (url2); // create the target folder file2.mkdirs (); // traverse the source folder file [] files = file1.listfiles (); For (int A = 0; A <files. length; A ++) {// It is a standard file, and we will directly copy the action if (files [A]. isfile () {// confirm the location where the target file needs to be copied. It must be in the target folder file target = new file (file2, files [A]. getname (); copyfile (files [a], target);} If (files [A]. isdirectory () {// The folder is still a folder. At this time, get the file Folder path string source1 = url1 + file. separator + files [A]. getname (); string target1 = url2 + file. separator + files [A]. getname (); copydir (source1, target1) ;}} Private Static void copydir (string source1, string target1) throws ioexception {File Source = new file (source1 ); file target = new file (target1); target. mkdirs (); file [] files = source. listfiles (); For (int A = 0; A <files. length; A ++) {If (files [A]. isfil E () {file TARGET2 = new file (target, files [A]. getname (); copyfile (files [a], TARGET2);} If (files [A]. isdirectory () {string soure3 = source1 + file. separator + files [A]. getname (); string target3 = target1 + file. separator + files [A]. getname (); // recursion. The copydir method is called for folder. The above if condition is recursive exit copydir (soure3, target3 );}}} private Static void copyfile (File file, file target) throws ioexception {buffered Inputstream Bis = new bufferedinputstream (New fileinputstream (File); bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (target); byte [] Buf = new byte [1024]; int Len = 0; while (LEN = bis. read (BUF ))! =-1) {Bos. Write (BUF, 0, Len);} bis. Close (); Bos. Close ();}}

Copy folders using Io streams (the folder contains subdirectories)

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.