Use Java local copy files and folders
How do I achieve a copy of the folder? What should I do if there is a folder in the folder?
Next, I will demonstrate my method to some beginners, I hope to help you!
/ *
* Copyright by
http://www.designac.org/
* /
Package CopyFile;
Import java.io. *;
/ **
* @Author administrator
*
* Class Function:
* /
Public class copydirectory {
Public static void main (string args []) throws oException {
String Url1 = "C: / Downloads / Kele";
String Url2 = "D: / java / copyfile / copy";
(New file (url2)). MKDIRS ();
File [] File = (New File (URL1)). Listfiles ();
For (int i = 0; i IF (file [i] .isfile ()) { FileInputStream Input = New FileInputStream (file [i]); FileOutputStream Output = New FileoutputStream (URL2 "/" file [i] .getname ()); Byte [] b = new byte [1024 * 5]; Int Len; While ((len = INPUT.READ (B))! = - 1) { Output.write (B, 0, LEN); } Output.flush (); Output.close (); INPUT.CLOSE (); } IF (file [i] .Indirectory ()) { CopyDirectiory (URL2 "/" File [i] .getname (), URL1 "/" file [i] .getname ()); } } } Public static void copydirectiory (string file1, string file2) throws oException { NEW file (file1)). MKDIRS (); File [] file = (new file (file2)). Listfiles (); For (int i = 0; i IF (file [i] .isfile ()) { FileInputStream Input = New FileInputStream (file [i]); FileOutputStream Output = New FileoutputStream (file1 "/" file [i] .getname ()); Byte [] b = new byte [1024 * 5]; Int Len; While ((len = INPUT.READ (B))! = - 1) { Output.write (B, 0, LEN); } Output.flush (); Output.close (); INPUT.CLOSE (); } IF (file [i] .Indirectory ()) { Copydirectiory (file1 "/" file [i] .getname (), file2 "/" file [i] .getname ()); } } } }