当前位置:网站首页>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呢? ︎
边栏推荐
猜你喜欢
Toronto Research Chemicals农药检测丨甲硫威
机器人控制器编程实践指导书旧版-实践八 机器人综合设计
VoLTE基础自学系列 | 3GPP规范解读之Rx接口(上集)
报告详解影响英特尔10/11/12代酷睿处理器的ÆPIC Leak安全漏洞
电路板ROHS测试报告怎么办理?电路板ROHS检测流程
FFmpeg 从mp4上提取H264的nalu
如何通过JMobile软件实现虹科物联网HMI/网关的报警功能?
const的自己理解
HarmonyOS自动化测试框架—Hypium
Before opening a futures account, you must confirm the handling fee as soon as possible
随机推荐
StoneDB 文档捉虫活动第一季
1720. 解码异或后的数组
set和map使用讲解
机器人控制器编程实践指导书旧版-实践六 LCD液晶显示(点阵)
NPDP|传统行业产品经理如何进行能力提升?
【HMS core】【FAQ】AR Engine、Analytics Kit、Video Editor Kit、Image Kit、Map Kit典型问题合集2
【数据存储精讲】整型和浮点型有什么区别?为什么会精度丢失?
【测试】黑盒测试用例设计方法
20220810
stm32中的CAN通讯列表模式配置解析与源码
Toronto Research Chemicals BTK抑制剂丨ACP-5197
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
MongoDB教程
AVFrame相关api内存管理
flex使用align-content无效
【FAQ】HarmonyOS ETS如何给组件设置边框
【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(下)
企业即时通讯是什么?可以应用在哪些场景?
容器化 | 在 S3 实现定时备份
【图像去雾】基于颜色衰减先验的图像去雾附matlab代码