Java File replication

Source: Internet
Author: User

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 (); }        }    }

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.