Java--io class, Byte throttle buffer

Source: Internet
Author: User

two ways to compare byte stream reading data
read one byte at a time read one byte array at a time        ? Improves operational efficiency by reading multiple data at a time
Public class Copydemo { //First step: Read the contents of 1.txt into memory FileInputStream ///Step two: Read the in-memory data into the 2.txt FileOutputStream //Use a byte stream to copy things will not appear garbled, because it is to copy all the things past, did not come out to parse Public static void Main (string[] args) throws IOException { FileInputStream fis = new FileInputStream ("My skateboard Shoes. mp4"); //Any file format can be copied FileOutputStream fos = new FileOutputStream ("Kaobei.mp4"); //Method One: Inefficient, one byte at a time / * int b = 0; While ((b = Fis.read ())! =-1) { Fos.write (b);                 }*/ //Method Two: High efficiency, an entire array at a time byte[] bytes = new byte[1024]; int len = 0; While (len = fis.read (bytes))! =-1) { Fos.write (Bytes,0,len);                 } fis.close (); Fos.close ();         } }




byte buffer stream (wrapper stream, with some buffers added 8092 byte)
byte stream the speed of reading and writing an array is significantly faster than reading and writing one byte at a time, which is a buffer effect added to the array, and Java itself is designed with this in mind, so it provides a bytes buffer stream byte buffered output stream         ? Bufferedoutputstream Bufferedinputstream (InputStream in); //Buffer size default is 8192 Create a Bufferedinputstream and save its arguments, which is input stream in for future use. Bufferedinputstream (inputstream in, int size); creates a value with the specified buffer size.
byte buffered input stream         ? Bufferedinputstream Bufferedoutputstream (OutputStream out); creates a new buffered output stream to write data to the specified underlying output stream. Bufferedoutputstream (outputstream out, int size); creates a new buffered output stream that writes data with the specified buffer size to the specified underlying output stream.
Public class Buffertestmain { Public static void Main (string[] args) throws IOException { FileInputStream fis = new FileInputStream ("My skateboard Shoes. mp4"); //Such FIS do not have buffers Bufferedinputstream Bfis = new Bufferedinputstream (FIS); FileOutputStream fos = new FileOutputStream ("Kaobei.mp4"); Bufferedoutputstream Bfos = new Bufferedoutputstream (FOS);
//Method One:/* int b = 0;While ((b = Bfis.read ())! =-1) {//appears to be a byte-by-byte read, in fact the system implementation is read 8,192 bytes to the buffer at a timeBfos.write (b); }*///Method two:Faster, the buffer comes with a 8192 buffer, and itself defines a 1024 buffer byte[] bytes = new byte[1024]; int len = 0;While (len = bfis.read (bytes))! =-1) {Bfos.write (bytes, 0, Len); }         } }

Java--io class, Byte throttle buffer

Related Article

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.