当前位置:网站首页>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
边栏推荐
猜你喜欢
JMeter 常用的几种断言方法,你会几种呢?
解决:Hbuilder工具点击发行打包,一直报尚未完成社区身份验证,请点击链接xxxxx,项目xxx发布H5失败的错误。
redis stream 实现消息队列
并查集介绍和基于并查集解决问题——LeetCode 952 按公因数计算最大组件大小
尚医通【预约挂号系统】总结
25.时间序列预测实战
设计信息录入界面,完成人员基本信息的录入工作,
秒懂大模型 | 3步搞定AI写摘要
redis---分布式锁存在的问题及解决方案(Redisson)
IDEA引入类报错:“The file size (2.59 MB) exceeds the configured limit (2.56MB)
随机推荐
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
小程序如何使用订阅消息(PHP代码+小程序js代码)
redis stream 实现消息队列
经典动态规划问题的递归实现方法——LeetCode39 组合总和
MySQL BIGINT 数据类型
经典递归回溯问题之——解数独(LeetCode 37)
【虚幻引擎UE】UE5实现WEB和UE通讯思路
无人驾驶运用了什么技术,无人驾驶技术是
form表单提交到数据库储存
金仓数据库 KDTS 迁移工具使用指南 (5. SHELL版使用说明)
LeetCode每日五题01:两数之和 (均1200题)
两日总结六
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
关于常用状态码4XX提示错误
【并发】概念
中职网络安全竞赛C模块MS17-010批量扫描
给Unity Behavior Designer(Unity行为树) 的Can See Object 画圆锥辅助图
Distributed Computing MapReduce | Spark Experiment
Linux之Redis 缓存雪崩,击穿,穿透
8.2学习记录