当前位置:网站首页>The difference between character stream and byte stream
The difference between character stream and byte stream
2022-08-04 08:01:00 【Fairy wants to carry】
Differences:
The byte stream itself does not use the buffer (memory) when operating, it is directly operated by the file itself, while the character stream uses the buffer when operating, and then operates the file through the buffer

byte stream not close
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 the File class to find a fileFile f = new File("d:" + File.separator + "test.txt"); // declare the File object// Step 2: Instantiate the parent class object through the child classOutputStream out = null;// prepare an output objectout = new FileOutputStream(f);// instantiate via object polymorphism// Step 3: Do the write operationString str = "Hello World!!!";// prepare a stringbyte b[] = str.getBytes();// string to byte arrayout.write(b);// output the content// Step 4: Close the output stream// out.close();// not closed at this time}} 
At this time, the byte stream operation is not closed, but the output content still exists in the file, which proves that the byte stream directly manipulates the file itself.The following continues to use the character stream to complete, and then observe the effect.
Use character stream not close
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 the File class to find a fileFile f = new File("d:" + File.separator + "test.txt");// declare the File object// Step 2: Instantiate the parent class object through the child classWriter out = null;// prepare an output objectout = new FileWriter(f);// instantiate via object polymorphism// Step 3: Do the write operationString str = "Hello World!!!";// prepare a stringout.write(str);// output the content// Step 4: Close the output stream// out.close();// not closed at this time}} After the program runs, you will find that there is no content in the file. This is because the buffer is used in the character stream operation, and When the character stream is closed, the content in the buffer will be forced to be output, but if the program is not closed, the contents of the buffer cannot be output, so it is concluded that the buffer is used by the character stream, and the buffer is not used by the byte stream.

Buffer:
Answer: A buffer can be simply 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), the performance will be very low. At this time, in order to improve performance, a part of the data can be temporarily read into an area of memory, and then directlyIt is enough to read data from this area, because the reading speed of memory will be faster, which can improve the performance of the program.
Flush:
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 is not handled// Step 1: Use the File class to find a fileFile f = new File("d:" + File.separator + "test.txt");// declare Fileobject// Step 2: Instantiate the parent class object through the child classWriter out = null;// prepare an output objectout = new FileWriter(f);// instantiate via object polymorphism// Step 3: Do the write operationString str = "Hello World!!!";// prepare a stringout.write(str);// output the contentout.flush();// Forced to clear the contents of the buffer// Step 4: Close the output stream// out.close();// not closed at this time}} 
Concept:
1. All files on the hard disk or in transmission are in bytes, including pictures, etc.are stored in bytes, and characters are only formed in memory, so in development, byte stream is widely used;
2. Byte stream is the most basic, all subclasses of InputStream and OutputStream are, mainly used to process binary data, it is processed by bytes;
3. These two are associated with InputStreamReader and OutputStreamWriter, and actually are associated with byte[] and String. The problem of Chinese characters in actual development is actually the conversion between character stream and byte streamcaused by inconsistency
4. When converting from byte stream to character stream, it is actually when byte[] is converted to String, public String(byte bytes[], StringcharsetName)
Stream classification:
1. Java byte stream
InputStream is the ancestor of all byte input streams, and OutputStream is the ancestor of all byte output streams.
2. Java's character stream
Reader is the ancestor of all read string input streams, and writer is the ancestor of all output strings.
InputStream, OutputStream, Reader, writer are abstract classes.So you can't directly new
边栏推荐
猜你喜欢
随机推荐
小程序如何使用订阅消息(PHP代码+小程序js代码)
【我想要老婆】
LeetCode 135. 分发糖果
解决报错: YarnScheduler: Initial job has not accepted any resources
串口监听 - 软件方案
【UE虚幻引擎】UE5三步骤实现AI漫游与对话行为
LeetCode 97. 交错字符串
1161. Maximum Level Sum of a Binary Tree
轻量化Backbone VGNetG成就“不做选择,全都要”轻量化主干网络
leetcode 22.7.31(1)两数之和 (2)整数除法
怎么写专利更容易通过?
【字符串】最小表示法
「PHP基础知识」转换数据类型
分布式计算实验3 基于PRC的书籍信息管理系统
powershell和cmd对比
登录拦截实现过程
JNI学习1.环境配置与简单函数实现
10个程序员可以接私活的平台和一些建议,赚麻...
data:image/jpg; base64 format data is converted to image
Mysql insert on duplicate key 死锁问题定位与解决









