当前位置:网站首页>3、IO流之字节流和字符流
3、IO流之字节流和字符流
2022-08-04 20:42:00 【Code攻城狮】
1、IO流是做什么的?
2、IO的分类:
按流向分:可以分成读和写
按类型分,可分为字符流和字节流
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("罗兆你要自强".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 字节流写时候-换行操作
window:系统下 \r\n、\r、\n 3种方式(推荐使用\r\n,因为另外2种换行符可能不适用于Win7、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 返回-1说明读取到行尾了,所以只要返回的不是-1就可以继续读。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lpq6TPHZ-1659543003410)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f59f75469f024ba78da47522215da434~tplv-k3u1fbpfcp-watermark.image?)]](/img/69/55cb637bc7ac7547759e7d8f8e900b.png)
5.4 字节流循环读取
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 根据数组的长度,决定一次读取多少字节,并把读取到数据存到数组中。
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;//记录读取数据的位置
while ((len= fis.read(bys))!=-1){
String str=new String(bys,0,len);
System.out.println(str);
}
6、字符流
字符流=字节流+编码表
当我们调用read 方法 读取字符的时候
实际上,底层会先读取到(一个字节)
判断这个字节是否为负数(中文字符,字节的第一个,肯定是负数)
你:-74 -73 -72
第一次读取到的是-74,就可以判断,读取的内容是中文了
是中文的话,再结合平台默认编码表,决定继续读取多少字节
UTF-8,读取3个字节—>转换为中文的汉子
如果读取到的字节,是正数,就直接读取一个字节,然后转换。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v5F7f6P7-1659543003412)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3d565c27297640698dfb9acf53f54ebd~tplv-k3u1fbpfcp-watermark.image?)]](/img/3b/4f582075eb39f636e49e82ff01fe44.png)
6.1 拷贝一个大文本文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3AS0nMRp-1659543003412)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/23a1911a3aba4959ab0e5cd68095b133~tplv-k3u1fbpfcp-watermark.image?)]](/img/de/12a576c4c66d8f92c9a6df6ed3251a.png)
7、字节流和字符流的区别
7.1、创建的数组类型不一样
7.2 字节流写出的数据,就算没有关闭流,数据也会写出。字符流不会。
7.3 字符流写出数据可以直接写出字符串。
7.4 字符流只有调了close关闭流后,或者flush 才会把缓冲区刷出。
flush把数据从缓冲区刷到文件中后,还可以继续进行刷出。
但是close后,就不能再进行写出了。
以上内容,来自我稀土掘金博客。
链接: 稀土掘金
边栏推荐
猜你喜欢

二叉搜索树解决硬木问题

Tensorflow2 环境搭建

ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享

DICOM医学影像协议
![[Academic related] Tsinghua professor persuaded to quit his Ph.D.:I have seen too many doctoral students have mental breakdowns, mental imbalances, physical collapses, and nothing!...](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Academic related] Tsinghua professor persuaded to quit his Ph.D.:I have seen too many doctoral students have mental breakdowns, mental imbalances, physical collapses, and nothing!...

vscode离线安装插件方法

C语言之实现扫雷小游戏

CAS :80750-24-9(脱硫生物素 NHS 酯)

使用百度EasyDL实现森林火灾预警识别

AWS SES 的监控和告警
随机推荐
用 Excel 爬取网络数据的四个小案例
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
深度解析:为什么跨链桥又双叒出事了?
composition-api
c语言小项目(三子棋游戏实现)
mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
使用 Allatori 进行 Jar 包混淆
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
Unreal 本地化 国家化 多语言
【debug】postgres数据存储错乱
工龄10年的测试员从大厂“裸辞”后...
如何进行AI业务诊断,快速识别降本提效增长点?
链路聚合技术及VRRP
xss课堂内容复现
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
Zero-knowledge proof - zkSNARK proof system
[Data Mining] Written Exam Questions for Sohu Data Mining Engineers
刷题-洛谷-P1200 你的飞碟在这儿Your Ride Is Here
使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题