File class -- copy a specified folder

Source: Internet
Author: User

Enter the target folder to be copied.

Enter the destination folder to be copied

Public class copydir {static partition in = new partition (system. in); static file dir = NULL; public static void main (string [] ARGs) {system. out. println ("Enter the path of the target folder to be copied:"); string need = in. nextline (); Dir = new file (need); // dir = new file ("F: \\ 0123"); string fordir = dir. getpath (); system. out. println ("Enter the path to the destination folder to be copied:"); string destdir = in. nextline (); file DEST = new file (destdir); // encapsulate the destination folder into a file object. This mainly determines Whether the target folder exists. // copy the target folder to the folder. // file DEST = new file ("F :"); /// \ myserver // copy all files and folders in the target folder to the folder. The name of this folder may be the same as destdir = DeST. getpath () + "\" + dir. getname (); If (! DeST. isdirectory () {system. out. println ("Enter the path of a folder"); return ;} // determine whether the target folder contains the else {// the following code is written only when you want to practice file filtering to determine whether the target folder contains the target folder //// if DEST is a file rather than a folder, The listfiles method will throw the nullpointerexception. // file [] files = DeST. listfiles (New filenamefilter () {// public Boolean accept (File Dir, string name) {// system. out. println (name); // DIR is DEST, so name is the name of all files in DEST, including folder and file // return name. equals (copydir. this. dir. getnam E (); //}); // system. out. println (files. length ); //// when the length is equal to 1, it indicates that the destination folder to be copied has a folder with the same name, that is, the copied folder is in the copied folder, and a folder copy is created at this time. // If (files. length = 1) {// destdir = DeST. getpath () + "\" + dir. getname () + "-Copy"; // determines whether the destination folder contains the target folder and whether the target folder exists by checking the file todir = new file (destdir); If (todir. exists () {destdir = DeST. getpath () + "\" + dir. getname () + "-Copy"; todir = new file (destdir);} int COUNT = 0; while (todir. exists () {count ++; If (! Destdir. contains ("-copy (" + (count-1) + ") destdir = DeST. getpath () + "\" + dir. getname () + "-copy (1)"; elsedestdir = destdir. replace ("-copy (" + (count-1) + ")", "-copy (" + Count + ")"); todir = new file (destdir );} todir. mkdir ();} If (dir. isdirectory () {system. out. println ("Start copying... "); Copydir (Dir, fordir, destdir);} else {system. Out. println (" You are not entering a folder! "); Return;} system. out. println ("target Folder:" + dir. getname () + "-> copy to target Folder:" + destdir + ", copied successfully");} public static void copydir (File Dir, string fordir, string destdir) {file [] files = dir. listfiles (); For (File file: Files) {// note that you must first create a new folder with the same name as the target of the folder and then recursion if (file. isdirectory () {// copy the files in the folder to another folder. before copying the files, you must create a folder named file todir = new file (file. getpath (). replace (fordir, destdir); todir. mkdir (); // create a folder and recursive copydir (File, fordir, destdir);} else {// copy the new file tofile = new file (file. getpath (). replace (fordir, destdir); try {tofile. createnewfile (); fileinputstream FS = new fileinputstream (File); fileoutputstream Fos = new fileoutputstream (tofile); byte [] Buf = new byte [1024]; int Len = 0; while (LEN = FCM. read (BUF ))! =-1) {FOS. write (BUF, 0, Len);} FCM. close (); FOS. close ();} catch (exception e) {e. printstacktrace ();}}}}}


/* The copy folder reads the file from the original folder and assigns the value to the folder copy. In fact, it copies the file to the created folder copy. 1. Read the data from the original folder FS [I]. isfile () FS [I] is a file that is directly copied 2, when FS [I] is a folder, first create a subfolder copy and then copy the files in the atomic folder to the folder copy. Then, define a string storage file path */class in the parent folder copy. copydir {string S = "null "; // The outermost directory path string Path = "null"; // defines the path of a string storage file public void readdir (file F, int column) throws exception // The parameter F indicates the original folder {lev++; If (lev== 1) {S = f. getname (); system. out. println (s); Path = f. getpath (). replac Eall (S, S + "copy"); system. out. println (PATH); file dir = new file (PATH); system. out. println (dir. mkdir ();} // file [] FS = f. listfiles (); For (INT I = 0; I <FS. length; I ++) {If (FS [I]. isfile () {Path = FS [I]. getparent (). replaceall (S, S + "copy"); file fc = new file (path, FS [I]. getname (); // before creating a file, make sure that the path (the folder where the file is located) is stored; otherwise, an exception FC is thrown. createnewfile (); // copy the file to the new file. bufferedinputstream Bis = new bufferedinputstream (New filei Nputstream (FS [I]); bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (FC); byte [] Buf = new byte [1024]; int Len = 0; while (LEN = bis. read (BUF ))! =-1) {Bos. write (BUF, 0, Len);} Bos. close (); bis. close () ;}// create a folder copy if (FS [I]. isdirectory () {Path = FS [I]. getpath (). replaceall (S, S + "copy"); file dir = new file (PATH); // create the folder dir. mkdir (); readdir (FS [I], lev) ;}} public static void main (string [] ARGs) throws exception {file F = new file ("F: \ 0123 "); long start = system. currenttimemillis (); New copydir (). readdir (F, 0); long over = system. currenttimemillis (); system. out. println ("folder copied successfully, time consumed:" + (over-Start ));}}


This article comes from "either desperate or rolling back !" Blog, please be sure to keep this source http://jiangzuun2014.blog.51cto.com/8732469/1532185

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.