Modified CopyFile Class

Source: Internet
Author: User

This is the modified CopyFile class, the previous class has limitations, it cannot copy large files

This is my first time to write a practical application of the class, thank the bloggers for their selfless dedication, thank Seayxu Teacher's Point

But this class is not perfect, it is not a problem to copy files, but if it is copied folder, the space occupied by the copy will be slightly different from the original, but does not affect the integrity of the data (file size, but occupy a different space)

The completion of this class also marks the beginning of the learning of file operations, into the socket field

This class is mainly composed of 4 public methods and two private methods, although the class is very long, but the essence is only the CopyData (File,file) method

 Packagecom.test;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;/*** The Main method is only 4, through a certain logic to determine the specific method of call *@authorDawn **/ Public classCopyfiletwo {/*** Copy the file in the same directory, plus the naming logic of the file name *@paramSrcfilepath Source file path *@paramdestfilepath destination file path*/     Public Static voidCopyfilesamepath (String srcfilepath,string destfilepath) {File srcfile=NewFile (Srcfilepath); File DestFile=NewFile (Destfilepath); if(!srcfile.exists ()) {System.out.println ("Source file does not exist"); return; }        if(!(Srcfile.getpath (). Equals (Destfile.getpath ()))) {System.out.println ("Method call Error: Copy only files in the same directory"); return; }        if(!Srcfile.isfile ()) {System.out.println ("Method call Error: Source file is not a single file"); return; } File newFile=naming (DestFile); Try{newfile.createnewfile (); }Catch(IOException e) {e.printstacktrace ();    } copydata (Srcfile,newfile); }    /*** Secondary method for naming new files and folders under the same directory *@paramDestFile A reference to the target file class *@returnreturns a reference to the renamed file class*/    Private StaticFile naming (file destfile) {file NewFile=NULL; intincreasing=2; String folder=destfile.getparent (); String FileName= "Copy" +Destfile.getname (); String NewPath=folder+file.separator+FileName; NewFile=NewFile (NewPath);  while(Newfile.exists ()) {FileName= "Copy" +increasing++ + "" +Destfile.getname (); NewPath=folder+file.separator+FileName; NewFile=NewFile (NewPath); }        returnNewFile; }    /*** Secondary method, specifically used to transfer data *@paramFile class reference for srcfile source file *@paramdestfile the file class reference of the target files*/    Private Static voidCopyData (File srcfile,file destfile) {Try{FileInputStream Reader=NewFileInputStream (srcfile); FileOutputStream writer=NewFileOutputStream (destfile); intLength=0; byte[] databytes=New byte[4096];  while((Length=reader.read (databytes))!=-1) {writer.write (databytes,0, length);            } reader.close ();        Writer.close (); }Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); }    }    /*** Copy files in different directories *@paramSrcfilepath Source file path *@paramDestfilepath Destination file path *@paramoverlay whether to overwrite*/     Public Static voidCopyfiledifferentpath (String srcfilepath,string Destfilepath,Booleanoverlay) {File Srcfile=NewFile (Srcfilepath); File DestFile=NewFile (Destfilepath); if(!srcfile.exists ()) {System.out.println ("Source file does not exist"); return; }        if(Srcfile.getpath (). Equals (Destfile.getpath ())) {System.out.println ("Method call Error: can only be copied to a different directory"); return; }        if(!Srcfile.isfile ()) {System.out.println ("Method call Error: File can only be copied"); return; }        if(!(Srcfile.getname (). Equals (Destfile.getname ()))) {System.out.println ("Input Error: File name is not the same"); return; }        if(Destfile.exists ()) {if(overlay) {destfile.delete (); }Else{System.out.println ("Duplicate file name"); return; }        }        Try{destfile.createnewfile (); }Catch(IOException e) {e.printstacktrace (); return;    } copydata (Srcfile,destfile); }    /*** Copy folders in different directories *@paramSrcfolderpath source Folder path *@paramDestfolderpath Destination Folder path *@paramoverlay whether to overwrite*/     Public Static voidCopyfolderdifferentpath (String srcfolderpath,string Destfolderpath,Booleanoverlay) {File Srcfolder=NewFile (Srcfolderpath); File Destfolder=NewFile (Destfolderpath); if(!srcfolder.exists ()) {System.out.println ("Source file does not exist"); return; }        if(Srcfolder.getpath (). Equals (Destfolder.getpath ())) {System.out.println ("Method call Error: can only be copied to a different directory"); return; }        if(!srcfolder.isdirectory ()) {System.out.println ("Method call Error: Only folder can be copied"); return; }        if(!(Srcfolder.getname (). Equals (Destfolder.getname ()))) {System.out.println ("Input Error: File name is not the same"); return; }        if(Destfolder.exists ()) {if(overlay) {file[] files=Srcfolder.listfiles (); intSize=files.length;  for(inti=0;i<size;i++){                    if(Files[i].isfile ()) {Copyfiledifferentpath (Files[i].getpath (), Destfolder.getpath ( )+file.separator+files[i].getname (),true); }Else{Copyfolderdifferentpath (Files[i].getpath (), Destfolder.getpath ()+file.separator+files[i].getname (),true); }                }            }Else{System.out.println ("Folder name duplicate"); return; }        }Else{destfolder.mkdirs (); Copyfolderdifferentpath (Srcfolderpath,destfolderpath,true); }    }     Public Static voidCopyfoldersamepath (String srcfolderpath,string destfolderpath) {File Srcfolder=NewFile (Srcfolderpath); File Destfolder=NewFile (Destfolderpath); if(!srcfolder.exists ()) {System.out.println ("Source folder does not exist"); return; }        if(!(Srcfolder.getpath (). Equals (Destfolder.getpath ()))) {System.out.println ("Method call Error: Copying only folders in the same directory"); return; }        if(!srcfolder.isdirectory ()) {System.out.println ("Method call Error: Source file is not a single folder"); return; } File NewFolder=naming (Destfolder);        Newfolder.mkdirs (); file[] Files=Srcfolder.listfiles (); intSize=files.length;  for(inti=0;i<size;i++){            if(Files[i].isfile ()) {Copyfiledifferentpath (Files[i].getpath (), Newfolder.getpath ( )+file.separator+files[i].getname (),false); }Else{Copyfolderdifferentpath (Files[i].getpath (), Newfolder.getpath ()+file.separator+files[i].getname (),false); }        }            }    /*** Test with Main method *@paramargs*/     Public Static voidMain (string[] args) {Copyfilesamepath ("F:\\world of warcraft\\data\\data\\data.019", "F:\\world of warcraft\\data\\data\\data.019"); }}

Modified CopyFile Class

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.