当前位置:网站首页>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流的体系结构:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jy0MFc2q-1659543003407)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bf40da15d11b4e43a6cc406bc7394e9c~tplv-k3u1fbpfcp-watermark.image?)]](/img/ff/f8a6f34fa5a680347afbcd70d487c0.png)
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();
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WEcd2Ksj-1659543003408)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/155008c0fd344a1ba19e748da09892a9~tplv-k3u1fbpfcp-watermark.image?)]](/img/4f/2b16a2af317a21be0117445359f328.png)
4.1、FileOutputStream的构造方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f3lJleZk-1659543003408)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/906072a373ee4182b1b53b9ec8f8528f~tplv-k3u1fbpfcp-watermark.image?)]](/img/8c/11d2ae97d97d64b751bb596521ef30.png)
4.2 FileOutputStream的成员方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lcHBxgTb-1659543003409)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3c9ec3963adf4220bbdae9d4052982ef~tplv-k3u1fbpfcp-watermark.image?)]](/img/3d/1696b510378b8f749de110c90b07af.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DiUnrO6F-1659543003409)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/78666df5ca7a42469f8527b43e503280~tplv-k3u1fbpfcp-watermark.image?)]](/img/83/f18677deb1bc5120e81ea1adb55d56.png)
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 一次读取一个字节
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r9kLdch6-1659543003410)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/304a5de4145a44a1b8bc7c993acb6db1~tplv-k3u1fbpfcp-watermark.image?)]](/img/9d/7042990461baa5e22116fe660cae5d.png)
5.2 构造方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tn76gb14-1659543003410)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bfadb3e8a4564eb3907cf4015b39ff18~tplv-k3u1fbpfcp-watermark.image?)]](/img/cd/2b852f2e6b21970f661398ea87e676.png)
5.3 返回-1Description read to the end of the line,So as long as the returned is not-1Just keep reading.
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lpq6TPHZ-1659543003410)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f59f75469f024ba78da47522215da434~tplv-k3u1fbpfcp-watermark.image?)]](/img/69/55cb637bc7ac7547759e7d8f8e900b.png)
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);
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bqfQ9A3K-1659543003411)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1476aeb434b1476aa33dd09b4b372496~tplv-k3u1fbpfcp-watermark.image?)]](/img/88/0b2dba58d9e97858d7dcd64733c3ae.png)
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
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2kvYRGDh-1659543003411)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d7039304b8b94cc4b83dd556e6b63647~tplv-k3u1fbpfcp-watermark.image?)]](/img/1e/bc10625dae499559d76b68cd27b932.png)
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
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Nz3nTrvv-1659543003411)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/95814b5b066e4fb3b01fb88a272e3064~tplv-k3u1fbpfcp-watermark.image?)]](/img/42/191481b123fe238fa2d2b66790d7ad.png)
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,然后转换.
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v5F7f6P7-1659543003412)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3d565c27297640698dfb9acf53f54ebd~tplv-k3u1fbpfcp-watermark.image?)]](/img/3b/4f582075eb39f636e49e82ff01fe44.png)
6.1 Copy a large text file
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3AS0nMRp-1659543003412)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/23a1911a3aba4959ab0e5cd68095b133~tplv-k3u1fbpfcp-watermark.image?)]](/img/de/12a576c4c66d8f92c9a6df6ed3251a.png)
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.
链接: 稀土掘金
边栏推荐
猜你喜欢
随机推荐
EasyUi常用代码
C语言——青蛙跳台阶(递归)
机器学习_02
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
win10终端中如何切换磁盘
1、File对象学习
【TypeScript】深入学习TypeScript枚举
链路聚合技术及VRRP
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
win10 uwp modify picture quality compress picture
取证程序分类
深度解析:为什么跨链桥又双叒出事了?
【数据挖掘】搜狐公司数据挖掘工程师笔试题
工龄10年的测试员从大厂“裸辞”后...
知识分享|如何设计有效的帮助中心,不妨来看看以下几点
vscode离线安装插件方法
无代码平台字段设置:基础设置入门教程
C语言基础[通俗易懂]
3、IO流之字节流和字符流
Nuxt.js的优缺点和注意事项









