The simplest iostream--file copy

Source: Internet
Author: User

/*Key: "In" refers to the memory, "out" refers to the outside of memory*/ImportJava.io.*; Public classmyreadfile{ Public Static voidMain (string[] args) {Try{            /*The first step is to locate the data source && destination file*/File F=NewFile ("D:/lab_2/test.txt"); File F2=NewFile ("D:/lab_2/test2.txt"); /*Step Two, build the input && output pipeline*/FileInputStream fis=NewFileInputStream (f); FileOutputStream Fos=NewFileOutputStream (F2); /*step three, Operation Pipeline*/             for(; fis.available () > 0;) Fos.write (Fis.read ()); }Catch(Exception e) {} }}

Because it is a byte of one byte read and write, when the file is slightly larger (greater than 1MB), the speed is very slow.

You can read and write 1000 bytes at a time with a slight modification.

byte New byte [+];              for (; fis.available () > 0;)                fis.read (TMP);                Fos.write (TMP);

It is said that 2 of the n-th computer processing efficiency will be faster.

            byte New byte [8192];              for (; fis.available () > 0;)                fis.read (TMP);                Fos.write (TMP);                                    

Finally, the Standard Edition.

/*step three, Operation Pipeline*/            byte[] tmp =New byte[8192]; //handle most of the content            intLength = fis.available ()/8192;  for(inti = 0; i < length; i + +) {fis.read (TMP);            Fos.write (TMP); }            //processing the last remaining content            intSize =Fis.read (TMP); Fos.write (TMP,0, size);
/*
The last sentence is changed to:
int left_length = fis.availble ();

Fis.read (TMP, 0, left_length);

Fos.write (TMP, 0, left_length);

There's no big problem.
*/

The simplest iostream--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.