当前位置:网站首页>3. Byte stream and character stream of IO stream
3. Byte stream and character stream of IO stream
2022-08-04 20:55:00 【Code Siege Lion】
1、IOWhat does a stream do?
2、IO的分类:
按流向分:Can be divided into read and write
按类型分,It can be divided into character stream and byte stream
3、IO流的体系结构:
4、字节流写出数据:FileOutputStream
//1、创建字节输出流对象
FileOutputStream fileOutputStream=new FileOutputStream("a.txt");
//2、写出数据
fileOutputStream.write(97);
fileOutputStream.write(98);
fileOutputStream.write(99);
fileOutputStream.write("Luo Zhao, you have to be self-improvement".getBytes(StandardCharsets.UTF_8));
//3、关闭流,释放资源.
fileOutputStream.close();
4.1、FileOutputStream的构造方法
4.2 FileOutputStream的成员方法
4.3 When the byte stream is written-换行操作
window:系统下 \r\n、\r、\n 3种方式(推荐使用\r\n,因为另外2Such newlines may not applyWin7、Win8)
mac:\r
linux:\n
5、字节输入流(读取)FileInputStream
5.1 一次读取一个字节
5.2 构造方法
5.3 返回-1Description read to the end of the line,So as long as the returned is not-1Just keep reading.
5.4 The byte stream is read cyclically
FileInputStream fis=new FileInputStream("a.txt");
int i;
while ((i=fis.read())!=-1){
System.out.println(i);
}
5.5 数组拷贝 读取-写出
FileInputStream fis=new FileInputStream("D:\learing\test.mp4");
FileOutputStream fos=new FileOutputStream("test2.mp4");
byte[] bys=new byte[1024];
int len;
while ((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fis.close();
fos.close();
5.6 根据数组的长度,Determines how many bytes to read at a time,And store the read data into an array.
5.6.1
FileInputStream fis=new FileInputStream("a.txt");
byte[] bys=new byte[3];
int read = fis.read(bys);
System.out.println(read);
System.out.println(Arrays.toString(bys));
5.6.2
FileInputStream fis=new FileInputStream("a.txt");
byte[] bys=new byte[3];
int len;//Record the location where the data was read
while ((len= fis.read(bys))!=-1){
String str=new String(bys,0,len);
System.out.println(str);
}
6、字符流
字符流=字节流+编码表
当我们调用read 方法 读取字符的时候
实际上,The bottom layer will be read first(一个字节)
Check whether this byte is negative(中文字符,the first of the bytes,肯定是负数)
你:-74 -73 -72
第一次读取到的是-74,就可以判断,The content read is in Chinese
是中文的话,Combined with the platform default encoding table,Decide how many bytes to continue reading
UTF-8,读取3个字节—>Hanzi converted to Chinese
If bytes are read,是正数,Just read a byte directly,然后转换.
6.1 Copy a large text file
7、字节流和字符流的区别
7.1、The created array types are not the same
7.2 The data written out of the byte stream,Even without closing the stream,Data is also written out.character stream will not.
7.3 Character stream to write out data can directly write out a string.
7.4 The character stream is only tunedclose关闭流后,或者flush The buffer will be flushed out.
flushAfter flushing the data from the buffer to the file,You can also continue to brush out.
但是close后,Can no longer be written out.
以上内容,From my Rare Earth Nuggets blog.
链接: 稀土掘金
边栏推荐
猜你喜欢
刷题-洛谷-P1179 数字统计
Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership
WIN10系统如何开启终端
Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!
SAP ABAP OData 服务如何支持 $select 有选择性地仅读取部分模型字段值试读版
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
使用堡塔应用管理器配置laravel队列方法
win10终端中如何切换磁盘
[TypeScript] In-depth study of TypeScript enumeration
多商户商城系统功能拆解22讲-平台端分销商品
随机推荐
该如何训练好深度学习模型?
遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了
Tensorflow2 环境搭建
web 应用开发最佳实践之一:避免大型、复杂的布局和布局抖动
Oreo domain name authorization verification system v1.0.6 public open source version website source code
vim clear last search highlighting
[21天学习挑战赛——内核笔记](二)——设备树基础
How to make good use of builder mode
Chrome安装zotero connector 插件
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
【数据挖掘】搜狐公司数据挖掘工程师笔试题
后缀式的计算
web漏洞扫描器-awvs
经验分享|盘点企业进行知识管理时的困惑类型
如何找到某个 ABAP structure 某字段的源头来自哪个数据库表
长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析、生物量估算与趋势分析等领域中的应用
拒绝服务攻击DDoS介绍与防范
Five Minutes Introductory Text Processing Three Musketeers grep awk sed
【2022牛客多校5 A题 Don‘t Starve】DP
win10终端中如何切换磁盘