This article describes the differences and usage of Java character streams and byte streams. Share it for your reference, as follows:
The main difference between byte streams and character streams is their processing method
Stream classification:
1. Java's byte stream
InputStream is the ancestor of all byte input streams, while OutputStream is the ancestor of all byte output streams.
2. Java character stream
Reader is the ancestor of all read string input streams, while writer is the ancestor of all output strings.
InputStream, OutputStream, Reader, and writer are all abstract classes. So you can't just new
Byte streams are the most basic. All subclasses of InputStream and OutputStream are mainly used to process binary data. They are processed by bytes, but in reality, a lot of data is text. The concept of character streams is proposed. They are processed according to the encode of the virtual machine, that is, to convert the character sets. The two are related through InputStreamReader and OutputStreamWriter, and are actually related through byte[] and String.
The Chinese character problems that arise in actual development are actually caused by inconsistent conversion between character streams and byte streams.
When converting from byte stream to character stream, it is actually when byte[] is converted to String.
public String(byte bytes[], String charsetName)
There is a key parameter character set encoding, which we usually omit, so the system uses the operating system lang
When a character stream is converted into a byte stream, it is actually when a String is converted into byte[],
byte[] String.getBytes(String charsetName)
The same is true as for many other streams in java.io, mainly to improve performance and use convenience.
Such as: BufferedInputStream, PipedInputStream, etc.
Specific implementation
Byte Stream FileInputStream FileOutputStream
Character Streaming FileReader FileWriter
The unit of character stream processing is 2 byte Unicode characters, which operates characters, character arrays or strings respectively, while the unit of byte stream processing is 1 byte, which operates byte and byte arrays. Therefore, the character stream is made up of a Java virtual machine converting bytes into 2 bytes of Unicode characters, so it is more supportive for multiple languages! If it is an audio file, picture, or song, use byte streaming, and if it is related to Chinese (text), use character streaming.
All files are stored in bytes. What is retained on disk is not the characters of the file, but the characters are encoded into bytes first, and then the bytes are stored on disk. When reading files (especially text files), they are also read byte byte to form a sequence of bytes.
A byte stream can be used for any type of object, including binary objects, while a character stream can only process characters or strings; 2. A byte stream provides the function of handling any type of IO operation, but it cannot directly process Unicode characters, while a character stream can do it.
Byte streams can be converted into character streams using InputSteamReader OutputStreamWriter
Convert to BufferdReader BufferedWriter They have buffers for example: Read files from byte stream input to character stream input define a byte stream:
FileInputStream fileInputStream = new FileInputStream("d:/text.txt");// Define a byte stream pointing to D:/TEXT.TXT InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);// Convert byte stream to InputStreamReaderBufferedReader bufferedReader = new BufferedReader(inputSteamReader);// InputStreamReader converts to bufferedReader with cacheYou can assign the read content to characters
String ss = new String();String s;while((s = bufferedReader.readLine())!=null){ ss += s;}FileInputStream fileInputStream = new FileInputStream("d:/text.txt"); // Define a byte stream pointing to D:/TEXT.TXT InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); // Convert byte stream to InputStreamReader BufferedReader bufferedReader = new BufferedReader(inputSteamReader); //InputStreamReader is converted into a bufferedReader with cache. You can assign the read content to the characters String ss = new String(); String s; while((s = bufferedReader.readLine())!=null){ ss += s; }For example: Write a file from the byte stream to the character stream output
FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt");//Define a file pointing to D:/TEXT.TXT OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(s);bufferedWriter.close();outputStreamWriter.close();fileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //Define a file pointing to D:/TEXT.TXT OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(s);bufferedWriter.close();outputStreamWriter.close();fileOutputStream.close();Routine:
Convert strings to byte streams#region Convert strings to byte streams
/**/ /// <summary>//// Convert a string into a byte stream/// </summary>/// <param name="_Source"> String</param>/// <returns> Byte stream</returns>public static byte [] String2Bytes( string strSource){ System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(memoryStream); binaryWriter.Write( strSource ); byte [] buffer = memoryStream.GetBuffer(); return buffer;}#endregionConvert byte streams to strings #region Convert byte streams to strings
/**/ /// <summary>//// Convert byte streams into strings/// </summary>/// <param name="bytData"> Byte streams</param>/// <returns> String</returns>public static string Bytes2String( byte [] bytData){ // Byte stream->String System.IO.MemoryStream memoryStream2 = new System.IO.MemoryStream(bytData); System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(memoryStream2); string s2 = binaryReader.ReadString(); return s2;}#endregionI hope this article will be helpful to everyone's Java programming.