当前位置:网站首页>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
边栏推荐
- 分布式计算实验4 随机信号分析系统
- Distributed Computing Experiment 2 Thread Pool
- 全国职业院校技能大赛网络安全竞赛之应急响应
- 24.循环神经网络RNN
- 字节跳动岗位薪酬体系曝光,看完我真的酸了...
- inject() can only be used inside setup() or functional components.
- 为什么手动启动GBase 8c数据库中GTM节点,起不来。显示“Run cmd failed:scp: /tmp/gtm_gtm1.server: Permission denied”
- 【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
- 中职网络安全竞赛C模块MS17-010批量扫描
- 两日总结八
猜你喜欢
随机推荐
千万级别的表分页查询非常慢,怎么办?
【CNN基础】转置卷积学习笔记
QT + msvc2017编译器
MMDetection finetune
轻量化Backbone VGNetG成就“不做选择,全都要”轻量化主干网络
一天学会JDBC04:ResultSet的用法
一天学会JDBC06:PrepaerdStatemtnt
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
安装GBase 8c数据库集群时,报错误码:80000306,显示Dcs cluster not healthy。怎么处理错误呢?
<jsp:useBean>动作的使用
金仓数据库 KDTS 迁移工具使用指南 (4. BS 版使用说明)
尚医通【预约挂号系统】总结
一天学会JDBC03:Statement的用法
设计信息录入界面,完成人员基本信息的录入工作,
10个程序员可以接私活的平台和一些建议,赚麻...
最强分布式锁工具:Redisson
给Unity Behavior Designer(Unity行为树) 的Can See Object 画圆锥辅助图
JNI学习1.环境配置与简单函数实现
【UE虚幻引擎】UE5实现动态导航样条线绘制
【字符串】最小表示法








