Selection of character stream and byte stream

Source: Internet
Author: User

First, when to use a character stream, when to use a byte stream? What is the basis?

    1. Scenarios for using character streams: If you are reading character data, use a character stream.
    2. Use a byte stream scenario: If the read-write data does not need to be converted into characters, use a byte stream.

Ii. the problem of copying pictures using character stream

 Public classCopyimage { Public Static voidMain (string[] args)throwsIOException {//Locate the target fileFile infile=NewFile ("E:\\1.jpg"); File Descfile=NewFile ("E:\\2.jpg"); //build data input and output pipelineFileReader filereader=NewFileReader (InFile); FileWriter FileWriter=NewFileWriter (Descfile); //creating a buffer character array for edge-read-side writing        Char[] buf=New Char[1024]; intLength=0;  while((Length=filereader.read (BUF))!=-1) {filewriter.write (buf,0, length); }        //Close the data channelFilewriter.close ();    Filereader.close (); }}

Problem: The copy of the picture is smaller than the original, lost some bytes, the picture is damaged, why?

Conclusion: If you copy a picture, use a byte stream.

Selection of character stream and byte 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.