当前位置:网站首页>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.
链接: 稀土掘金
边栏推荐
- 嵌入式分享合集28
- Using Baidu EasyDL to realize forest fire early warning and identification
- Oreo domain name authorization verification system v1.0.6 public open source version website source code
- 新式茶饮,卷完水果还能卷什么?
- 【C语言】指针和数组的深入理解(第三期)
- 【1403. 非递增顺序的最小子序列】
- MySQL field type
- uwp ScrollViewer content out of panel when set the long width
- vehemently condemn
- Tensorflow2 环境搭建
猜你喜欢
随机推荐
Feign 与 OpenFeign
Latex分章节、分段落编译:input{}与include{}的区别
3、IO流之字节流和字符流
IPV6地址
ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
[AGC] Build Service 1 - Cloud Function Example
括号匹配
【C语言】指针和数组的深入理解(第三期)
后缀式的计算
如何最简单、通俗地理解爬虫的Scrapy框架?
顺序队列
文章复现:超分辨率网络-VDSR
用 Excel 爬取网络数据的四个小案例
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
Using Baidu EasyDL to realize forest fire early warning and identification
漫画 | 老板裁掉我两周后,又把我请回去,工资翻番!
遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了
搭建MyCat2双主双从的MySQL读写分离
刷题-洛谷-P1200 你的飞碟在这儿Your Ride Is Here