The difference between character stream and byte stream in Java

Source: Internet
Author: User

A character stream processes a cell that is 2-byte Unicode characters, manipulating characters, character arrays, or strings, and a byte-stream processing unit of 1 bytes, manipulating bytes and byte arrays. So a character stream is a character that is converted from a Java virtual machine to a 2-byte Unicode character, so it's better for multi-language support! If it is audio files, pictures, songs, use a word throttle better, if it is related to Chinese (text), with a character stream better. All files are stored in bytes (byte), save the characters on the disk instead of the file character, and then store the bytes to disk by first encoding the characters into bytes. When reading a file (especially a text file), it is also a byte-by-byte read to form a sequence of bytes.1The byte stream can be used for any type of object, including binary objects, while character streams only handle characters or strings;2The byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly, and a character stream is available. The main difference between a byte stream and a character stream is that their processing style is the most basic, all inputstream and OutputStream subclasses are, mainly used in processing binary data, it is processed in bytes, but in fact a lot of data is text, but also put forward the concept of character stream, It is handled by the encode of the virtual machine, that is, the conversion of the character set is converted between the two by Inputstreamreader,outputstreamwriter, in fact, by byte[] and string. In the actual development of the Chinese garbled problem is in the character stream and byte stream between the conversion of non-uniform caused. Conversion: When converting from byte to stream, actually byte[] is converted to string, PublicString (bytebytes[], string charsetname) has a key parameter character set encoding, which is usually omitted, when the system is using the operating system Lang and when the character flow into a byte stream, actually a string converted to byte[],byte[] string.getbytes (String charsetname) is the same Truth Java instance: Io divided into two streams byte stream inputstream outputstream character Stream Reader Writer they are abstract class Byte stream FileInputStream fileoutputstream character streams filereader FileWriter byte flow change to a character stream can be converted to B by Inputsteamreader OutputStreamWriter Ufferdreader BufferedWriter They have buffers the following examples illustrate://read file from byte stream input to character stream input Public Static voidwriter () {FileInputStream FileInputStream=NULL; InputStreamReader InputStreamReader=NULL; BufferedReader BufferedReader=NULL; FileOutputStream FileOutputStream=NULL; OutputStreamWriter OutputStreamWriter=NULL; BufferedWriter BufferedWriter=NULL; Try{  //defines a pointer to D:/text. TXT Byte streamFileInputStream =NewFileInputStream ("D:\\test.txt"); //Convert bytes to InputStreamReaderInputStreamReader =NewInputStreamReader (FileInputStream); //InputStreamReader converted to BufferedReader with cacheBufferedReader =NewBufferedReader (InputStreamReader); //You can assign the read content to a characterString str =NewString ();   String Line; //reads a line of characters from the second   while(line = Bufferedreader.readline ())! =NULL) {str+ = line +"\ n"; } System. out. println (str); //writes a file from a byte stream output to a character stream outputFileOutputStream =NewFileOutputStream ("D:\\test_new.txt"); OutputStreamWriter=NewOutputStreamWriter (FileOutputStream); BufferedWriter=NewBufferedWriter (OutputStreamWriter);  Bufferedwriter.write (str); } Catch(IOException ex) {ex.printstacktrace ();}finally{  Try{   if(NULL!=FileInputStream) fileinputstream.close (); if(NULL!=InputStreamReader) inputstreamreader.close (); if(NULL!=BufferedReader) bufferedreader.close (); if(NULL!=FileOutputStream) fileoutputsream.close (); if(NULL!=outputstreamwriter) outputstreamwriter.close (); if(NULL!=bufferedwriter) bufferedwriter.close (); } Catch(IOException ex) {ex.printstacktrace (); } }}

The difference between character stream and byte stream in Java

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.