当前位置:网站首页>2022-08-09 Study Notes day32-IO Stream
2022-08-09 Study Notes day32-IO Stream
2022-08-10 18:32:00 【aggressive leeks】
IO流
学习内容
什么是IO?
I:input (输入)
O: output (输出)
IO:通过ioTo complete the file to read and write
什么是IO流?
Is memory and hard disk space between a file通道,Can complete the file to read and write
流的分类
按照流向 (以内存为参照物)
From the hard disk space ———> 内存 (输入/读)
从内存 ———> 硬盘空间 (输出/写)按照操作单元(读取数据方式)
字节流 ——> 按字节读取 (Can operate all of the files)
字符流——> 按字符读取 (只能操作文本文件[.txt/.html/.java等])按角色划分
节点流:Direct manipulation is a specificIO流
处理流:在节点流的基础上,做进一步的处理当一个流的构造方法中需要一个流的时候,This was reported in flow is called node flow
外部负责包装的流叫做包装流/处理流,Node flow and packing flow is relative
Colseable接口
所有的流都实现类Colseable接口,Say they are可关闭的close()
close() ----> 关闭流
- 所有的流(In addition to the standard output stream)都需要手动关闭 (Java7之前)
- To ensure that the flow must be shut down,Don't close the flow takes up a lot of resources,可以将close()放到finally语句块中
- Convection is closed,Not empty judgment to do first,避免NPE
- 流的关闭顺序:先开后关,后开先关
- The closing of the packing flow and node flow:只需要关闭包装流即可
- 对于输出流来说,使用close()After you don't need to inflush()
AutoCloseable接口(Java7)
由于Closeable继承了AutoCloseable接口,表示所有的流,都是Can be automatically shut down
使用新语法:try-with-resource
-----> 在try的”()“中声明并初始化实现AutoClosealbe接口的流.
public class Demo {
@Test
public void testAutoColse() {
byte[] datas = {
73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
// 新语法 try-with-resource
try (OutputStream out = new FileOutputStream("1.txt")) {
out.write(datas);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在Java9之后,对try-with-resourceGrammar simplified,”()“里面可以是一个变量,但必须是final的或者等同final才行.
public class Demo {
@Test
public void testAutoColse() throws FileNotFoundException {
byte[] datas = {
73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
OutputStream out = new FileOutputStream("1.txt");
try (out) {
out.write(datas);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Flushable接口
所有的输出流(InputStream/Writer)都实现了Flushable接口,Said all the output stream is可刷新的flush()
flush() ----> 刷新流
注意close()具有flush()的功能
flush()Said it will channel surplus not output data output(清空管道),如果没有flush()可能会丢失数据
具体可以看下面的例子
public class Demo {
@Test
public void testFlush() throws IOException{
char[] datas = {
73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
BufferedWriter writer = new BufferedWriter(new FileWriter("1.txt"));
writer.write(datas);
// writer.flush();
}
}

Flow of the four family leader
字节流
java.io.InputStream ----> 字节输入流
java.io.OutputStream ----> 字节输出流
字符流
java.io.Reader ----> 字符输入流
java.io.Writer ----> 字符输出流
需要掌握的16个流
文件专属
FileInputStream类
包路径:java.io.FileInputStream
继承关系:

构造方法

方法

FileOutputStream类
包路径:java.io.FileOutputStream
继承关系

构造方法(The output stream initialization time——重点)

FileReader类
- 包路径:java.io.FileReader
- 继承关系

- For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation
FileWriter类
包路径:java.io.FileWriter
继承关系

For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation
缓冲流
Take the buffer output stream to rememberflush()或close(),否则会导致数据丢失
BufferedInputSteam类
- 包路径:java.io.BufferedInputStream
- 继承关系

- 方法
BufferedOutputStream类
- 包路径:java.io.BufferedOutputStream
- 继承关系

BufferedReader类
包路径:java.io.BufferedReader
继承关系

构造方法

方法

BufferedWriter类
- 继承关系
- 构造方法

- 方法

转换流
将字节流转换成字符流:字节流 ----> 转换流(字符流) 可以指定字符集
InputStreamReader类
包路径:java.io.InputStreamReader
继承关系

构造方法

指定字符集
public class Demo01 {
@Test
public void testInputStreamReader() throws IOException {
// Did not specify the character set,使用默认字符集(此处是UTF-8),在使用reader()Methods while reading the file data,以UTF-8Character set file data for(解码)
InputStreamReader reader1 = new InputStreamReader(new FileInputStream("temp.txt"));
// 指定字GBK符集,在使用reader()Methods while reading the file data,以GBKCharacter set file data for(解码)
InputStreamReader reader2 = new InputStreamReader(new FileInputStream("temp.txt"),"GBK");
}
}
Read the file at the time of the garbled problems and solutions
案例:When the default character set for theUTF-8时,Try on thisGBKCode files to read
具体代码如下
public class Demo01 {
@Test
public void testInputStreamReader() {
// Did not specify the character set,使用默认字符集(此处是UTF-8),在使用reader()Methods while reading the file data,以UTF-8Character set file data for(解码)
try(InputStreamReader reader1 = new InputStreamReader(new FileInputStream("temp.txt"));) {
int data;
while ((data = reader1.read()) != -1) {
System.out.println(data + " ----> " + (char) data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果如下
为什么会出现乱码呢?
由于在readerInitialization time did not specify the character set,So the default character set(When the default hereUTF-8),所以使用readerTo read the byte stream,会使用用UTF-8字符集进行解码1,The file using the encoding ofGBK,Encoding and decoding the specified character set different,So the garbled words here.
- 设置默认字符集为GBK(不推荐)

将Global Encoding设置为GBK - 在InputStreamReaderInitialize the specified when the character set for theGBK

采用这两种方式,The file can be read normal
OutputStreamWriter类
- 包路径:java.io.OutputStreamWriter
- 继承关系

- 构造方法

指定字符集
public class Demo01 {
@Test
public void testOutputStreamWriter() throws FileNotFoundException {
// Did not specify the character set,使用默认字符集(此处是UTF-8),在使用writer()Method to write data into the file,以UTF-8Set the data(编码)
OutputStreamWriter writer1 = new OutputStreamWriter(new FileOutputStream("temp.txt"));
// Did not specify the character set,使用默认字符集(此处是UTF-8),在使用writer()Method to write data into the file,以UTF-8Set the data(编码)
OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream("temp.txt"));
}
}
注意:
如果文件不存在,Create the specified character set file
如果文件存在,Not set additional,Create the specified character set file and override the original file
如果文件存在,And set up additional,Does not overwrite the original file will not change the original file coded character set used
Written to the file of the code problems and solutions
数据流
标准输出流
注意:标准输出流不需要手动close(),但一定要flush(),Especially with the buffer flow
PrintStream类
- 包路径:java.io.PrintStream
- 继承关系

- For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation
We often use output statements"System.out.println()",Then use the standard output stream,其中"out"便是SystemUnder the class of a static property,这个“out”是PrintStream类型,The default content output to the console.Then we can change the output statements the output of the position that?答案是可以的,我们可以通过调用"System.setOut()"这个方法来设置out属性.
获取System.out
package com.jsoft.io;
import org.junit.Test;
import java.io.*;
public class Demo01 {
public void testPrintStream() {
// 获取System.out
PrintStream myOut = System.out;
// 向控制台输出内容
myOut.println("Pan her,我是你爹");
}
}
更改System.out (Change the output statements output location)
package com.jsoft.io;
import org.junit.Test;
import java.io.*;
public class Demo01 {
@Test
public void testPrintStream() throws FileNotFoundException {
PrintStream myOut = new PrintStream("temp.txt");
// 更改System.out的输出位置(控制台 ---> temp.txt)
System.setOut(myOut);
System.out.println("向temp.txt输出的内容");
// 将System.outTo modify the original output location(temp.txt ---> 控制台)
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
System.out.println("To the console output content");
}
}

PrintWriter类
包路径:java.io.PrintWriter
继承结构

For constructing method and the method,和PrintStream基本没啥区别,Specific content please see the help documentation
对象专属流
解码:The bytes in the specified character set into character
So what should we to readGBK呢? ︎
边栏推荐
- Mysql index, transaction and storage engine
- 企业即时通讯是什么?可以应用在哪些场景?
- 机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
- Go 语言快速入门指南:第四篇 与数据为舞之数组
- 背景视频铺满盒子
- StoneDB 文档捉虫活动第一季
- Making Pre-trained Language Models Better Few-Shot Learners
- H3C_堆叠(IRF)及链路聚合在项目中的综合应用
- Selenium - 如何使用隐式、显示、强制元素等待?
- pip安装时 fatal error C1083 无法打开包括文件 “io.h” No such file or directory
猜你喜欢

测试接口出现“data“: “Full authentication is required to access this resource“凭证已过期

破解校园数字安全难点,联想推出智慧教育安全体系

VoLTE基础自学系列 | 3GPP规范解读之Rx接口(上集)

Toronto Research Chemicals BTK甜味剂配方丨D-Abequose

【快应用】如何使用命令打包快应用rpk

Before opening a futures account, you must confirm the handling fee as soon as possible

Keil5退出仿真调试卡死的解决办法

开发模式对测试的影响

Mysql索引、事务与存储引擎

剖析Framework面试—>>>冲击Android高级职位
随机推荐
「企业架构」什么是Zachman框架?
Kong自定义插件初体验
Active users of mobile banking grew rapidly in June, hitting a half-year high
leet面试150
Interpretation of ZLMediaKit server source code---RTSP push and pull
实用工具在线网站
Scala中使用 Jackson API 进行JSON序列化和反序列化
「业务架构」业务能力的热图是什么,有啥用?
去除富文本标签样式
如何通过JMobile软件实现虹科物联网HMI/网关的报警功能?
欧洲核子研究中心首次在量子机器学习研究中取得实效
FFmpeg花屏解决(修改源码,丢弃不完整帧)
HarmonyOS自动化测试框架—Hypium
【2015】【论文笔记】等离子光混合器THz辐射的光谱——
五菱宏光MINI EV,唯一的缺点就是安全性
Before opening a futures account, you must confirm the handling fee as soon as possible
企业如何通过北森HR SaaS 自动化管理员工账号生命周期
电路板ROHS测试报告怎么办理?电路板ROHS检测流程
搭载2.8K 120Hz OLED华硕好屏 无畏Pro15 2022锐龙版屏开得胜
微服务架构-实现技术之六大基础组件:服务通信+事件驱动+负载均衡+服务路由+API网关+配置管理