当前位置:网站首页>2022/08/09 学习笔记 (day26) IO流
2022/08/09 学习笔记 (day26) IO流
2022-08-10 02:17:00 【激进的黄瓜】
目录
IO流
lO流全称是Input/Output Stream,译为“输入/输出流”
IO流概述和分类:
IO流介绍:
IO:输入/输出(Input/Output)
流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流 的本质是数据传输
IO流就是用来处理设备间数据传输问题的。常见的应用:文件复制;文件上传;文件下载
IO流的分类:
按照数据的流向:
输入流:读数据(读流)
输出流:写数据(写流)
按照数据类型来分:
字节流:
字节输入流
字节输出流
字符流:
字符输入流
字符输出流
IO流的使用场景:
1.如果操作的是纯文本文件,优先使用字符流
2.如果操作的是图片、视频、音频等二进制文件。优先使用字节流
3.如果不确定文件类型,优先使用字节流。字节流是万能的流
字节流写数据:
字节流抽象基类:
InputStream:这个抽象类是表示字节输入流的所有类的超类
OutputStream:这个抽象类是表示字节输出流的所有类的超类
字节输出流:
FileOutputStream(String name):创建文件输出流以指定的名称写入文件
字节输入流FileInputStream:
/***************创建输入流InputStream-begin********************/
FileInputStream in = null;
try {
in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(0);
}
/***************创建输⼊入流InputStream-end********************/
long num = 0; //用于计数的变量,作⽤是计算⼀共读了了多少个字节
int b = 0; //用于记录读取位置的变量
try {
/***************读取⽂件-begin********************/
while((b = in.read()) != -1){
System.out.println((char)b);
num ++;
}
in.close();
System.out.println("共读取了"+num+"个字节");
/***************读取文件-end********************/
} catch (IOException e) {
System.out.println("⽂文件读取错误");
System.out.println(0);
}
字节输出流FileOutputStream:
/***************创建输出流FileOutputStream-begin********************/
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(0);
}
/***************创建输出流FileOutputStream-end********************/
int b = 0;
try {
/***************拷⻉文件-begin********************/
while((b = in.read()) != -1){
out.write(b);
}
in.close();
out.close();
/***************拷⻉文件-end********************/
} catch (IOException e) {
System.out.println("⽂文件复制错误");
System.exit(0);
}
字符输入流FileReader:
FileReader fr = null;
try {
fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
int c = 0;
while((c = fr.read()) != -1){
System.out.println((char)c);
}
fr.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定⽂件");
System.exit(0);
} catch (IOException e) {
System.out.println("⽂件读取错误");
System.exit(0);
}
字符输出流FileWriter:
FileWriter fw = null;
try {
fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
String str = "HelloWorld我的Java世界";
fw.write(str);
fw.close();
} catch (IOException e) {
System.out.println("⽂件写⼊入错误");
System.exit(0);
}
字节缓冲流:
BufferOutputStream:该类实现缓冲输出流。 通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
字节缓存输入流:
try {
FileInputStream in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
BufferedInputStream bis = new BufferedInputStream(in, 1024);
int b;
while((b = bis.read()) != -1){
System.out.println((char)b);
}
bis.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("⽂件没有找到");
} catch (IOException e) {
System.out.println("⽂件操作错误");
}
字节缓冲输出流:
try {
FileOutputStream out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
BufferedOutputStream bos = new BufferedOutputStream(out, 1024);
bos.write("helloMyJava".getBytes());
bos.close();
out.close();
} catch (FileNotFoundException e) {
System.out.println("⽂件没有找到");
} catch (IOException e) {
System.out.println("文件操作错误");
}
字符缓冲输入流:
try {
FileReader fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
BufferedReader br = new BufferedReader(fr, 1024);
String str;
while((str = br.readLine()) != null){
System.out.println(str);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("⽂件没有找到");
} catch (IOException e) {
System.out.println("⽂件操作错误");
}
字符缓冲输出流:
try {
FileWriter fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
BufferedWriter br = new BufferedWriter(fw);
br.write("来呀,相互伤害呀");
br.newLine();
br.write("来呀,相互伤害呀");
br.close();
fw.close();
} catch (FileNotFoundException e) {
System.out.println("⽂件没有找到");
} catch (IOException e) {
System.out.println("⽂件操作错误");
}
字节流写数据的两个小问题:
字节流写数据如何实现换行
1.windows:\r\n
2. linux:\n
3. mac:\r
思维导图:
边栏推荐
- The Evolutionary History of the "Double Gun" Trojan Horse Virus
- 【Kali安全渗透测试实践教程】第8章 Web渗透
- 【图像分类】2022-ResMLP
- 【红队】ATT&CK - 自启动 - 利用LSA身份验证包自启动机制
- 网页挖矿溯源?浏览器浏览历史查看工具Browsinghistoryview
- 【Kali安全渗透测试实践教程】第6章 密码攻击
- 实例045:求和
- 2022.8.8考试区域链接(district)题解
- Completion of the flag set in 2022
- Difference Between Data Mining and Data Warehousing
猜你喜欢
“双枪”木马病毒的进化史
Difference Between Data Mining and Data Warehousing
实例046:打破循环
Data Governance (5): Metadata Management
2022 Top Net Cup Quals Reverse Partial writeup
【二叉树-中等】2265. 统计值等于子树平均值的节点数
Anchor_generators.py analysis of MMDetection framework
【Kali安全渗透测试实践教程】第7章 权限提升
【二叉树-困难】124. 二叉树中的最大路径和
推荐几款好用的MySQL开源客户端,建议收藏
随机推荐
月薪35K,靠八股文就能做到的事,你居然不知道
Example 045: Summation
【二叉树-中等】2265. 统计值等于子树平均值的节点数
将信号与不同开始时间对齐
控制台中查看莫格命令的详细信息
The 25th day of the special assault version of the sword offer
数组(一)
实例047:函数交换变量
2022.8.8考试清洁工老马(sweeper)题解
Excel Advanced Drawing Skills 100 Lectures (23) - Countdown Counting in Excel
T5:Text-toText Transfer Transformer
Data Governance (5): Metadata Management
QT模态对话框及非模态对话框学习
what is eabi
跨站请求伪造(CSRF)攻击是什么?如何防御?
what is eabi
SQLserver加个判断
【二叉树-中等】508. 出现次数最多的子树元素和
what is a microcontroller or mcu
2022.8.9考试排列变换--1200题解