当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
(三)DDD上下文映射图——老师,我俩可是纯洁的男女关系!
两日总结七
Cross-species regulatory sequence activity prediction
【我想要老婆】
GBase 8c数据库集群中,怎么替换节点呢?比如设置A节点为gtm,换到B节点上。
GBase 8c中怎么查询数据库配置参数,例如datestyle。使用什么函数或者语法呢?
powershell和cmd对比
金仓数据库KingbaseES客户端编程接口指南-JDBC(9. JDBC 读写分离)
dalle:zero-shot text-to-image generation
金仓数据库的单节点如何转集群?
The national vocational skills contest competition of network security emergency response
华为设备配置VRRP与NQA联动监视上行链路
在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster”。这是为什么呢?有什么解决办法?
10个程序员可以接私活的平台和一些建议,赚麻...
a标签下载图片,不要预览
关于常用状态码4XX提示错误
将回调函数转为Flow
两日总结八
新特性解读 | MySQL 8.0 在线调整 REDO
秒懂大模型 | 3步搞定AI写摘要