In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how the difference between java byte stream and character stream is. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.
Byte streams are used very similarly to and character streams. Are there any other differences between the two besides the operation code?
In fact, byte stream itself does not use buffer (memory) during operation, it is directly operated by the file itself, while character stream uses buffer during operation, and then operates the file through buffer, as shown in the figure.
The following comparison is based on two write file operations, but the output stream is not closed after the byte stream and character stream operations are completed.
Example: Use byte stream without closing execution
package org.lxh.demo12.byteiodemo; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class OutputStreamDemo05 { public static void main (String[] args) throws Exception { //Exception thrown, not handled//Step 1: Use File class to find a file File f = new File ("d:" + File.separator + "test.txt"); //Declare File object//Step 2: Instantiate parent object by subclass OutputStream out = null; //Prepare an output object out = new FileOutputStream(f); //Instantiate by object polymorphism//Step 3: Write String str = "Hello World!!! "; //Prepare a string byte b[] = str.getBytes(); //String to byte array out.write(b); //Output contents//Step 4: Close output stream// out.close(); //Not closed at this time} }
Program Run Results:
Byte stream operation is not turned off at this time, but the output content still exists in the file, which proves that byte stream is directly operated on the file itself. And continue to use the following character stream to complete, and then observe the effect.
Example: Use character stream without closing execution
package org.lxh.demo12.chariodemo; import java.io.File; import java.io.FileWriter; import java.io.Writer; public class WriterDemo03 { public static void main (String[] args) throws Exception { //Exception thrown, not handled//Step 1: Use File class to find a file File f = new File ("d:" + File.separator + "test.txt");//Declare File object//Step 2: Instantiate parent object by subclass Writer out = null; //Prepare an output object out = new FileWriter(f); //Instantiate by object polymorphism//Step 3: Write String str = "Hello World!!! "; //Prepare a string out.write(str); //Output the content//Step 4: Close the output stream// out.close(); //Not closed at this time} }
Program Run Results:
After the program runs, it will find that there is no content in the file, because the character stream uses the buffer when operating, and the contents of the buffer will be forcibly output when the character stream is closed, but if the program is not closed, the contents of the buffer cannot be output, so it is concluded that the character stream uses the buffer, and the byte stream does not use the buffer.
Question: What is a buffer?
In many places have encountered the term buffer, so what exactly is a buffer? What's the use?
Answer: A buffer can simply be understood as a memory area.
A buffer can simply be understood as a special piece of memory.
In some cases, if a program frequently operates a resource (such as a file or database), performance will be very low, in order to improve performance, you can temporarily read part of the data into a memory area, and then directly read data from this area, because the read memory speed will be faster, so you can improve the performance of the program.
In the character stream operation, all characters are formed in memory, and all contents are temporarily stored in memory before output, so buffers are used to temporarily store data.
If you want to output all the contents of the character stream without closing it, you can use the flush() method in the Writer class to do so.
Example: Mandatory buffer emptying
package org.lxh.demo12.chariodemo; import java.io.File; import java.io.FileWriter; import java.io.Writer; public class WriterDemo04 { public static void main (String[] args) throws Exception { //Exception thrown does not handle//Step 1: Use File class to find a file File f = new File ("d:" + File.separator + "test.txt");//Declare File object//Step 2: Instantiate parent object by subclass Writer out = null; //Prepare an output object out = new FileWriter(f); //Instantiate by object polymorphism//Step 3: Write String str = "Hello World!!! "; //Prepare a string out.write(str); //Output the contents out.flush(); //Force the contents of the buffer empty//Step 4: Close the output stream// out.close(); //Not closed at this time} }
Program Run Results:
At this point, the content already exists in the file, which further proves that the content is stored in the buffer. This point should be especially noted in the future development of readers.
Question: Is it better to use byte stream or character stream?
After learning the basic operations of byte stream and character stream, I have roughly understood the differences between the operation flow, so is it better to use byte stream or character stream in development?
Answer: It is better to use byte streams.
Before answering, first explain to readers such a concept, all files in the hard disk or in the transmission are carried out in bytes, including pictures are stored in bytes, and characters are only formed in memory, so in the development, byte stream is widely used.
The main difference between byte streams and character streams is how they are processed
Java's byte stream InputStream is the ancestor of all byte input streams, while OutputStream is the ancestor of all byte output streams. Java's character stream Reader is the ancestor of all input streams of read strings, and writer is the ancestor of all output strings. InputStream, OutputStream,Reader, Writer are abstract classes. So it can't be new directly.
Byte stream is the most basic, all the subclasses of InputStream and OutputStream are mainly used to process binary data, it is processed by bytes, but in fact 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, to convert the character set. The two are associated through InputStreamReader,OutputStreamWriter, In fact, byte[] and String are used to associate Chinese characters that appear in actual development. In fact, the problem is caused by the inconsistency between the conversion between character stream and byte stream.
When converting from byte stream to character stream, it is actually byte[] to String. Public String(byte bytes[], String charsetName) has a key parameter character set code, which is usually omitted. The system uses lang of the operating system. When converting from character stream to byte stream, it is actually String to byte[]. The same is true for byte[] String.getBytes(String charsetName).
As for java.io, there are many other streams, mainly to improve performance and ease of use, such as Buffered InputStream,PipedInputStream, etc.
About java byte stream and character stream difference is how to share here, hope the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.