In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about the difference between byte stream and character stream in web. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, let's take a look at what a stream is:
All the data in the program is transmitted or saved in the way of stream. When the program needs data, it uses the input stream to read the data, and when the program needs to save some data, it uses the output stream. The input and output in the program are saved in the form of a stream, and all the files saved in the stream are actually byte files. To output a piece of binary data to a device one by one, or to read a piece of binary data one by one from a device, no matter what the input and output device is, we need to complete these operations in a unified way and describe them in an abstract way, which is called IO stream, and the corresponding abstract classes are OutputStream and InputStream. Different implementation classes represent different input and output devices. They all operate on bytes.
In applications, a piece of text that is entirely a character is often output or read in. Is it possible to use a byte stream? Everything in the computer ultimately exists in the form of binary bytes. For "China" characters, first get their corresponding bytes, and then write the bytes to the output stream. When reading, the first thing to read is a byte, but to display it as a character, we need to convert the byte to a character. Because of the wide range of such requirements, a wrapper class for character streams is specially provided. The underlying device always accepts only byte data, and sometimes to write a string to the underlying device, you need to convert the string into bytes before writing. The character stream is the wrapper of the byte stream, while the character stream accepts the string directly, which internally converts the string into bytes and then writes it to the underlying device, which makes it a little convenient for us to write or read strings to the IO. When you convert a character to a byte, you should pay attention to the problem of coding, because when a string is converted into a byte array, it is actually converted to a byte form of the character's encoding, and vice versa.
A code case that explains the relationship between byte stream and character stream:
Import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.InputStreamReader;import java.io.PrintWriter; public class IOTest {public static void main (String [] args) throws Exception {String str = "Chinese"; / * FileOutputStreamfos = newFileOutputStream ("1.txt"); fos.write (str.getBytes ("UTF-8")); fos.close (); * / * FileWriter fw = new FileWriter ("1.txt") Fw.write (str); fw.close (); * / PrintWriter pw = new PrintWriter ("1.txt", "utf-8"); pw.write (str); pw.close (); / * FileReader fr = new FileReader ("1.txt"); char [] buf = newchar [1024]; int len = fr.read (buf); String myStr = newString (buf,0,len); System.out.println (myStr); * / * FileInputStreamfr = new FileInputStream ("1.txt"); byte [] buf = newbyte [1024] Int len = fr.read (buf); String myStr = newString (buf,0,len, "UTF-8"); System.out.println (myStr); * / BufferedReader br = new BufferedReader (newInputStreamReader (newFileInputStream ("1.txt"), "UTF-8")); String myStr = br.readLine (); br.close (); System.out.println (myStr);} Thank you for reading! This is the end of the article on "what is the difference between byte stream and character stream in web". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.