Difference between byte stream and byte stream in Java

Source: Internet
Author: User

The byte stream processing unit is two bytes of Unicode characters, operating characters, character arrays or strings respectively, while the byte stream processing unit is one byte, operating byte and byte array. Therefore, the compaction stream is a character that is converted from a Java virtual machine to a Unicode Character in two bytes. Therefore, it is highly supported by many languages! For audio files, images, and songs, use text to reduce the cost. For Chinese (text) files, use text to improve the streaming speed.

All files are stored in bytes. What is stored on the disk is not the character of the file, but the characters are first encoded into bytes, and then stored on the disk. When reading a file (especially a text file), it is also a byte read to form a byte sequence.

1. byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings. 2. A byte stream provides the ability to process any type of IO operations, but it cannot directly process Unicode characters, while a bytes stream can.

The main difference between byte stream and byte stream is their processing method.

Byte streams are the most basic. All InputStream and OutputStream subclasses are used to process binary data. They are processed by byte.

However, in reality, a lot of data is text, and the concept of character stream is proposed. It is processed by the encode of the virtual machine, that is, Character Set conversion.

The two are converted using InputStreamReader and OutputStreamWriter. In fact, they are converted using byte [] and String. In actual development, Chinese garbled characters are caused by inconsistent conversion between the primary stream and the word throttling.

Conversion:

When the bytes are converted to bytes streams, the bytes [] is actually converted to strings,
Public String (byte bytes [], String charsetName)
There is a key parameter character set encoding, which is usually omitted. Then the system uses the lang of the operating system.
When the bytes stream is actually converted into byte,
The same applies to byte [] String. getBytes (String charsetName ).
Java example:
IO is divided into two streams
Byte stream InputStream OutputStream
Stream Reader Writer
They are all abstract classes.
Implementation
Byte stream FileInputStream FileOutputStream
Stream FileReader FileWriter
You can use InputSteamReader OutputStreamWriter to convert byte streams into bytes streams.
Convert to BufferdReader BufferedWriter they have Buffer

The following is an example:

// Read file input from byte stream to bytes Stream Input
Public static void writer (){
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;

FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
Try {
// Define a byte stream pointing to D:/TEXT. TXT
FileInputStream = new FileInputStream ("d :\\ test.txt ");
// Change byte Transfer to InputStreamReader
InputStreamReader = new InputStreamReader (fileInputStream );
// Convert InputStreamReader to bufferedReader with Cache
BufferedReader = new BufferedReader (inputStreamReader );

// Assign the read content to the character
String str = new String ();
String line;
// Read a line of characters from the second time
While (line = bufferedReader. readLine ())! = Null ){
Str + = line + "\ n ";
}
System. out. println (str );
 
// Write File output from byte stream to bytes stream output
FileOutputStream = new FileOutputStream ("d: \ test_new.txt ");

OutputStreamWriter = new OutputStreamWriter (fileOutputStream );

BufferedWriter = new BufferedWriter (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 ();
}
}
}

 

Related Article

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.