Write a simple file copy program in Java.

Source: Internet
Author: User

Code:

     Public Static voidcopyFile (String srcpath, String destpath) throws IOException {//the origin and purpose of establishing a file objectFile src =NewFile (Srcpath); File dest=NewFile (DestPath); //determines whether the input stream is a file because it can only copy files        if(!Src.isfile ()) {System. out. println ("only files can be copied!"); Throw NewIOException ("only files can be copied!"); }                //file input and outputInputStream is=NewFileInputStream (SRC); OutputStream OS=NewFileOutputStream (dest); //reads the file size and loops out the file        byte[] Flush =New byte[1024x768]; intLen =0;  while(-1! = (len = is. Read (flush))) {Os.write (flush,0, Len); }                //refreshes this output stream and forces all buffered output bytes to be written outOs.flush (); //freeing resources (typically open first and then off)Os.close ();  is. Close (); System. out. println ("Copy succeeded!"); }

Write a simple file copy program in Java.

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.