当前位置:网站首页>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
思维导图:

边栏推荐
猜你喜欢

《GB39707-2020》PDF download

实例046:打破循环

Will signal with different start time alignment

What makes training multi-modal classification networks hard?

【二叉树-中等】1261. 在受污染的二叉树中查找元素

【Kali安全渗透测试实践教程】第9章 无线网络渗透

MySQL: Introduction to Logging System | Error Log | Query Log | Binary Log: Bin-log Data Recovery Practice | Slow Log Query
![[Kali Security Penetration Testing Practice Course] Chapter 7 Privilege Escalation](/img/fe/c1aebd4a9f8be29820af35c79d6332.png)
[Kali Security Penetration Testing Practice Course] Chapter 7 Privilege Escalation

流星加速器木马分析与处置方案

数据挖掘和数据仓库之间的区别
随机推荐
[Red Team] ATT&CK - Self-starting - Self-starting mechanism using LSA authentication package
状态压缩小经验
P1564 膜拜
In automated testing, test data is separated from scripts and parameterized methods
网络爬虫错误
【QT】QT项目:自制Wireshark
【二叉树-简单】112. 路径总和
【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
flask增删改查
[Kali Security Penetration Testing Practice Tutorial] Chapter 6 Password Attack
将信号与不同开始时间对齐
控制台中查看莫格命令的详细信息
【Kali安全渗透测试实践教程】第7章 权限提升
MySQL:日志系统介绍 | 错误日志 | 查询日志 | 二进制日志:bin-log数据恢复实践 | 慢日志查询
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
数据库治理利器:动态读写分离
How to write a high-quality test case?
推荐几款好用的MySQL开源客户端,建议收藏
Deep Learning (5) CNN Convolutional Neural Network
网页挖矿溯源?浏览器浏览历史查看工具Browsinghistoryview