Mainly work code, no explanation.
/*** Copy file or folder source to Dest * <br> target file detection: * <br> A. When the file does not exist: need to create a file * <br> based on the suffix name to determine whether to create is a folder or a file, create a file with the suffix name * <br> If you create a folder, the source is copied to the Dest folder. * <br> If it is a file, copy the source directly to Dest * <br> B. When a file exists: you need to determine whether the destination file is a folder or a file * <br> if it is a folder, the source file name is named after it was created under that folder The file *@paramSource *@paramdest*/ Public Static voidMoveFile (File source,file dest)throwsfilenotfoundexception,ioexception{FileInputStream fis=NULL; FileChannel FC=NULL; FileOutputStream Fout=NULL; Writablebytechannel to=NULL; Try { if(!source.exists ()) {System.out.println ("The source file does not exist!"); return ; } if(Source.isdirectory ()) {file[] SRCs=Source.listfiles (); for(File src:srcs) {moveFile (src,NewFile (Dest.getpath () +file.separator+src.getname ())); } }Else{ if(!dest.exists ()) { if(!NewFile (Dest.getparent ()). Exists ()) { NewFile (Dest.getparent ()). Mkdirs (); } //creates a file with a suffix, depending on whether a suffix name is created to create a folder or a file. if(Dest.getname (). Matches (". *\\.. *") ) {dest.createnewfile (); }Else{dest.mkdir (); Dest=NewFile (Dest.getpath () +file.separator+source.getname ()); Dest.createnewfile (); } }Else{ //when the target file exists, you need to determine whether the destination file is a folder or a file, and if it is a folder, the file name of the source file is created under that folder . if(!dest.getname (). Matches (". *\\.. *") ) {dest=NewFile (Dest.getpath () +file.separator+source.getname ()); Dest.createnewfile (); }} FIS=NewFileInputStream (source); FC=Fis.getchannel (); Fout=NewFileOutputStream (dest); to=Fout.getchannel (); Fc.transferto (0, Fc.size (), to); Fis.close (); Fc.close (); Fout.flush (); Fout.close (); To.close (); } } Catch(FileNotFoundException e) {Throwe; } Catch(IOException e) {Throwe; } finally { if(fis!=NULL) {fis.close (); } if(fc!=NULL) {fc.close (); } if(fout!=NULL) {Fout.flush (); Fout.close (); } if(to!=NULL) {to.close (); } } }