Implementation of File copy

Source: Internet
Author: User

A copy of the file is equivalent to reading the source file and writing the destination together with the read file and writing to the file so the steps to copy the file are as follows: 1. Establish contact: Establish the relationship between the program and the source document and the purpose. 2. Select a stream: Two streams selected, input and output. such as: Fileinputstream/fileoutputstream3.                                Action: Read the source file, write to the destination such as a small piece of code in the program, while ( -1!= (len= input stream. Read (byte[] b)) {                         Output stream. Write (B,0,len);                        } output stream. Flush (); Close stream .... Omit 4. Release: close stream; source code: not encapsulated before://Establish contact
1File src=NewFile ("F:/a.txt");2File dest=NewFile ("F:/b.txt");3         //Select a stream4InputStream is=NULL;5OutputStream os=NULL;6         Try {7is=NewFileInputStream (SRC);8os=NewFileOutputStream (dest);9             //OperationTen             byte[] b=New byte[1024]; One             intlen=0;//actual Read length A              while( -1!= (len=Is.read (b))) { -Os.write (b, 0, Len);//It is recommended to use this write (byte[] b, int off, int len); -                 /*because, it is possible that the value of the B array is not full (1024) at the time of the last read, so this method is recommended. Avoid return the back some extra spaces,*/ -             } -Os.flush ();//Force Refresh -}Catch(FileNotFoundException e) { +SYSTEM.OUT.PRINTLN ("File not Found"); -}Catch(IOException e) { +System.out.println ("Read failed"); A e.printstacktrace (); at}finally{ -             Try { -                 if(NULL!=is ) { - is.close (); -                 } -                 if(NULL!=OS) { in os.close (); -                 } to}Catch(IOException e) { +System.out.println ("Shutdown stream failed"); -             } the}

After encapsulation:

1   Public voidcopyFile (String srcpath,string destpath) {2         //Establish contact3File src=NewFile (srcpath);4File dest=NewFile (destpath);5 copyFile (SRC, dest);6     }7      Public voidcopyFile (File src,file dest) {8         //Select a stream9InputStream is=NULL;TenOutputStream os=NULL; One         Try { Ais=NewFileInputStream (SRC); -os=NewFileOutputStream (dest); -             //Operation the             byte[] b=New byte[1024]; -             intLen=0; -              while( -1!= (len=Is.read (b))) { -Os.write (b, 0, Len); +             } -Os.flush ();//Force Refresh +}Catch(FileNotFoundException e) { ASYSTEM.OUT.PRINTLN ("File not Found"); at}Catch(IOException e) { -System.out.println ("Read failed"); - e.printstacktrace (); -}finally{ -             Try { -                 if(NULL!=is ) { in is.close (); -                 } to                 if(NULL!=OS) { + os.close (); -                 } the}Catch(IOException e) { *System.out.println ("Shutdown stream failed"); $             }Panax Notoginseng         } -}

Implementation of File copy

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.