Java implementation File replication

Source: Internet
Author: User

Directly on the source:

1  PackageCopyFile;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 /**9 * Preparatory work: Prepare a MP3 music, named 1.mp3. Copy the music file to the D drive. The path to the file is D:\1.mp3Ten * Mission Purpose: To achieve the replication of files. Copy the 1.mp3 to 2.mp3.  One  */ A  Public classFileCopy { -      Public Static voidMain (string[] args) { -FileInputStream FIS =NULL; theFileOutputStream fos =NULL; -File sourcefile =NewFile ("D:/1.mp3");//source File -File targetfile =NewFile ("D:/2.mp3");//target file -         byte[] buf =New byte[1024];//create a buffer of 1K size +         if(!sourcefile.exists ()) {//determine if the source file exists, exit the program if it does not exist -System.out.println ("Source file does not exist"); +             return; A         }         at         if(Targetfile.exists ()) {//when the target file is determined to exist, it is deleted. - Targetfile.delete (); -         } -         Try { -Targetfile.createnewfile ();//Create an empty destination file -}Catch(IOException e) { in e.printstacktrace (); -         } to         Try { +FIS =NewFileInputStream (sourcefile);//gets the input stream of the source file -FOS =NewFileOutputStream (targetfile);//gets the output stream of the destination file the              while(Fis.read (BUF)!=-1) {//while loop, the file starts copying * Fos.write (BUF); $             }Panax NotoginsengFis.close ();//close the input stream -Fos.close ();//turn off the output stream the}Catch(Exception e) { + e.printstacktrace (); A         } the     } +}

Java implementation File replication

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.