Java uses IO stream to copy photos complete examples and detailed analysis

Source: Internet
Author: User

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
/**

* Precautions:
* int Java.io.FileInputStream.read (byte[] b) throws IOException
* Official document Description of the method:
* Reads up to b.length bytes of the data from this input stream into an array of bytes.
* This method blocks until some input is available.
* That can be understood:
* FileInputStream will continuously read byte data into byte array B.
* Just read it once, then we perform a write operation accordingly:
* Fileoutputstream.write (Temp,0,len);
* What happens when this reading process terminates?
* When the end of the data has been read, there is an identifier-1; the expression has reached the end.
*
* A less appropriate understanding:
* FileInputStream will read byte data into byte array B again and again.
* After each reading, a little pause is performed, and then the
* Fileoutputstream.write (Temp,0,len);
* Perform data write operations.
*
* In fact, the following understanding is more appropriate:
* FileInputStream is constantly reading the data (rather than imagining it to be read again and again with a paused nature).
* But it must not exceed the size of b.length every time it is read, but it may not be the same length,
* For example: In most cases, the last time the number of bytes read will be less than the b.length size.
* Every time the Read () method is read, the data is stored in byte array B, and the method
* Also returns the number of bytes read (len).
* So we write to the data from O to Len in byte array B:
* Fileoutputstream.write (Temp,0,len);
*
* Now it's clear why use:
* Fileoutputstream.write (temp);
* is inaccurate and is likely to cause the copied photo to be larger than the original photo.
* Because Fileoutputstream.write (temp); method is always
* Write the size of the B-byte array, not how many are actually in B
Data.
* Such operations in the first few times there is no problem, because each loaded
* are all data of B-byte array size, but the last one is often loaded with dissatisfaction
* B. So each time it should be based on the actual data in B exactly how much to do
* operation is the use of fileoutputstream.write (Temp,0,len);
* can take into account every time.
*/
public class Copyphoto {
public static void Main (string[] args) {
Copyphoto Copyphoto = new Copyphoto ();
Copyphoto.copy ();
}

private void Copy () {
try {
FileInputStream fileinputstream=new FileInputStream ("d:\\c.jpg");
FileOutputStream fileoutputstream=new FileOutputStream ("d:\\new.jpg");
int len=0;
byte temp []=new byte[1024*8];;
while ((Len=fileinputstream.read (temp))!=-1) {
System.out.println ("len=" +len);
It's Right
Fileoutputstream.write (Temp,0,len);
It is wrong
Fileoutputstream.write (temp);
}
Fileoutputstream.close ();
Fileinputstream.close ();
} catch (Exception e) {
}

}

}

Java uses IO stream to copy photos complete examples and detailed analysis

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.