java--file operation character stream and byte stream difference

Source: Internet
Author: User

ext.: http://blog.csdn.net/joephoenix/articles/2283165.aspx
The IO stream of Java is divided into two kinds of streams

byte stream InputStream OutputStream

Character Stream Reader Writer

They're all abstract classes.

Specific implementation
byte stream FileInputStream FileOutputStream
Character Stream FileReader FileWriter

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 of the files are stored in byte (byte) storage, and the characters that are not files on the disk are encoded into bytes, and then the bytes are stored to disk. When reading a file (especially a text file), it is also a byte-by-byte read to form a sequence of bytes.
Byte streams can be used for any type of object, including binary objects, whereas character streams only handle characters or strings; 2. The 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.

Byte streams can be converted to character stream with Inputsteamreader OutputStreamWriter

Convert to Bufferdreader bufferedwriter they have buffers

Example: Read file from byte stream input to character stream input
Define a byte stream:

Defines a pointer to D:/text. TXT Byte stream
FileInputStream FileInputStream = new FileInputStream ("D:/text.txt");


Convert bytes to InputStreamReader
InputStreamReader InputStreamReader = new InputStreamReader (FileInputStream);

InputStreamReader converted to BufferedReader with cache
BufferedReader BufferedReader = new BufferedReader (Inputsteamreader);

You can assign the read content to a character

string ss = new string ();
String s;
while ((s = bufferedreader.readline ())!=null) {
SS + = s;
}

Example: Write file from byte stream output to character stream output

FileOutputStream FileOutputStream = new FileOutputStream ("D:/text.txt"); Defines a pointer to D:/text. TXT file

OutputStreamWriter outputstreamwriter = new OutputStreamWriter (FileOutputStream);

BufferedWriter bufferedwriter = new BufferedWriter (outputstreamwriter);

Bufferedwriter.write (s);

Bufferedwriter.close ();
Outputstreamwriter.close ();
Fileoutputstream.close ();

java--file operation character stream and byte stream difference

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.