Java Copy file (file input/output stream)

Source: Internet
Author: User

ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classCopyfiledemo { Public Static voidMain (string[] args) {CopyFile ("C:\\a.txt", "D:\\b.txt"); }        Private Static Final intOutput_file_buffer_size = 4*1024; Private Static BooleancopyFile (String srcdir,string desdir) {//file StreamFileInputStream FileInputStream =NULL; FileOutputStream FileOutputStream=NULL; //using the CacheBufferedinputstream bis;                Bufferedoutputstream Bos; //the length of each read        intReadlength = 0; //Current Position        intPosition = 0; //Printing InformationSystem.out.println ("CopyFile srcdir:" + Srcdir + "Desdir:" +Desdir); //target fileFile Disfileinfo =NULL; //Source file InformationFile FileInfo =NewFile (Srcdir); if(!fileinfo.exists ()) {System.out.println ("The source file does not exist return false"); return false; }                //Define Cache Size        byte[] buffer =New byte[Output_file_buffer_size]; Try{FileInputStream=NewFileInputStream (FileInfo); } Catch(FileNotFoundException e) {e.printstacktrace (); return false; }        //Buffered input streambis =NewBufferedinputstream (FileInputStream, 0x4000); //detects if the target file exists to create a fileDisfileinfo =NewFile (Desdir); if(!disfileinfo.exists ()) {            Try{disfileinfo.createnewfile (); } Catch(IOException e) {e.printstacktrace (); return false; }        }                Try{FileOutputStream=NewFileOutputStream (Disfileinfo); } Catch(FileNotFoundException e) {e.printstacktrace (); return false; }        //Buffered output streamBOS =NewBufferedoutputstream (FileOutputStream, 0x10000);  while(Position! =fileinfo.length ()) {            if(Position! =fileinfo.length ()) {                Try {                    //{@                    /*Core Code*/                    //reads each byte into the specified byte array at the given offset from this byte input stream bufferReadlength = bis.read (buffer, 0, output_file_buffer_size); //writes Len bytes from offset off to the specified byte array in buffer to this buffered output streamBos.write (buffer, 0, readlength); Position+=readlength; //@}}Catch(IOException e) {e.printstacktrace (); return false; }            }        }                //close the stream in turn        if(bis! =NULL){            Try{bis.close (); } Catch(IOException e) {e.printstacktrace (); return false; }        }                if(Bos! =NULL){            Try{bos.close (); } Catch(IOException e) {e.printstacktrace (); return false; }        }                if(Position = =fileinfo.length ()) {System.out.println ("CopyFile success!"); return true; }                return false; }}

Java Copy file (file input/output stream)

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.