当前位置:网站首页>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
边栏推荐
猜你喜欢
[Paper Notes] - Low Illumination Image Enhancement - Supervised - RetinexNet - 2018-BMVC
研究性学习专题 3_LL(1)语法分析设计原理与实现
华为设备配置VRRP与NQA联动监视上行链路
【论文笔记】—低照度图像增强—Supervised—RetinexNet—2018-BMVC
【UE虚幻引擎】UE5实现动态导航样条线绘制
【UE虚幻引擎】UE5三步骤实现AI漫游与对话行为
最强分布式锁工具:Redisson
leetcode 22.7.31(1)两数之和 (2)整数除法
leetcode 22.8.1 二进制加法
两日总结六
随机推荐
轻量化Backbone VGNetG成就“不做选择,全都要”轻量化主干网络
开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
高等代数_证明_两个矩阵乘积为0,则两个矩阵的秩之和小于等于n
Use of MotionLayout
Distributed Computing Experiment 4 Random Signal Analysis System
js异步变同步、同步变异步
24.循环神经网络RNN
unittest使用简述
redis stream 实现消息队列
七牛云上传图片和本地上传
Distributed Computing Experiment 1 Load Balancing
MySQL BIGINT 数据类型
金仓数据库KingbaseES客户端编程接口指南-JDBC(9. JDBC 读写分离)
Lightweight Backbone VGNetG Achieves "No Choice, All" Lightweight Backbone Network
【学习笔记】AGC036
[想要访问若依后台]若依框架报错401请求访问:error认证失败,无法访问系统资源
虚拟机没有USB网卡选项怎么解决
inject() can only be used inside setup() or functional components.
js-第一个出现两次的字母
redis---分布式锁存在的问题及解决方案(Redisson)