import java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import Java.util.Calendar; Public classSendserver {Private intnum =0; Public voidprocess () {Calendar Calendar=calendar.getinstance (); String dir= Calendar.Get(calendar.year) +""+ gettimestring (Calendar.Get(Calendar.month) +""); String OldPath="/img2"+dir; String NewPath="/img5"+dir; Try { while(true) {System. out. println ("Copy"+ OldPath +"Directory Start"); LongT1 =System.currenttimemillis (); Num=0; CopyFolder (OldPath, NewPath); Longt2 =System.currenttimemillis (); System. out. println ("Copy directory end, Time:"+ (T2-T1) +"MS, co-replication:"+ num +"file"); } } Catch(Exception ex) {ex.printstacktrace (); } } Public voidCopyFolder (String OldPath, String newpath) {Try{File Mfile=NewFile (NewPath); if(!mfile. Exists ()) { (NewFile (NewPath)). Mkdirs ();//Create a new folder if the folder does not exist} File a=NewFile (OldPath); string[] File=a.list (); File Temp=NULL; for(inti =0; i < file.length; i++) { if(Oldpath.endswith (file.separator)) {temp=NewFile (OldPath +File[i]); } Else{Temp=NewFile (OldPath + file.separator +File[i]); } if(Temp.isfile ()) {String FileName= NewPath +"/"+(Temp.getname ()). ToString (); File testfile=NewFile (fileName); if(!testfile.exists ()) {FileInputStream input=NewFileInputStream (temp); FileOutputStream Output=NewFileOutputStream (fileName); byte[] B =New byte[1024x768*5]; intLen; while(len = Input.read (b))! =-1) {Output.write (b,0, Len); } output.flush (); Output.close (); Input.close (); Num++; } } if(Temp.isdirectory ()) {//if it is a sub-folderCopyFolder (OldPath +"/"+ File[i], NewPath +"/"+File[i]); } } } Catch(Exception e) {System. out. println ("error Copying entire folder contents Operation"); E.printstacktrace (); } } Privatestring gettimestring (String time) {if(Time.length () <2){ return "0"+Time ; } Else{ returnTime ; } }}
Java copy a folder picture into another folder