First, when to use a character stream, when to use a byte stream? What is the basis?
- Scenarios for using character streams: If you are reading character data, use a character stream.
- 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