Java I/O框架
Java I/O 框架
目录
1. 流的概念
内存与存储设备之间传输数据的通道
2. 流的分类
- 按方向
- 输入流:将<存储设备>中的内容读到<内存>中
- 输出流:将<内存>中的内容读到<存储设备>中
- 按单位
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
- 按功能
- 节点流:具有实际传输数据的读写功能
- 过滤流:在节点流的基础之上增强功能
3. 字节流
- InputStream
- OutputStream
各种输入输出字节流的基类,所有字节流都继承这两个基类
4. FileInputStream
/**
* FileInputStream
*/
public class IOTest {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc.txt");
// 单个字节读取
// int data=0;
// while ((data=fis.read())!=-1){
// System.out.print((char) data);
// }
//一次读取多个字节
byte[] buf = new byte[3];
int count = 0;
while ((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));// new String(字节数组,开始的数据,截得数据长度)
}
fis.close();
}
}
5. FileOutputStream
public class IOTest2 {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc2.txt");
fos.write(97);
fos.write('b');
String string = "hello";
fos.write(string.getBytes());
fos.close();
}
}
Tips:FileOutputStream fos = new FileOutputStream("路径", true); // append=true, 表示可追加写,不覆盖原文件数据
6. 文件复制案例
// 1 创建流
// 1.1 文件字节输入流
FileInputStream fis = new FileInputStream("路径");
// 1.2 文件字节输出流
FileInputStream fos = new FileOutpuStream("路径");
// 2 边读边写
byte[] buf = new byte[1024];
int count = 0;
while((count = fis.read(buf)) != -1){
fos.write(buf, 0, count);
}
// 3 关闭
fis.close();
fos.close();
7. 字节缓冲流
-
特点
- 提高IO效率,减少访问磁盘次数
- 数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close
-
BufferedInputStream
public class IOTest3 {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int data=0;
while ((data=bis.read())!=-1){
System.out.println((char) data);
}
bis.close();
}
}
- BufferedOutputStream
public class IOTest4 {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/buffer.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
for (int i=0;i<10;i++){
bos.write("hello\r\n".getBytes());// 写入8k缓冲区
bos.flush();// 刷新到硬盘
}
//关闭
bos.close(); // 会自动flush
}
}
7. 对象流
ObjectOutputStream / ObjectInputStream
- 增强了缓冲区功能
- 增强了读写8种基本数据类型和字符串的功能
- 增强了读写对象的功能
- readObject(): 从流中读取一个对象
- writeObject(): 向流中写入一个对象
使用流传输对象的过程称为序列化、反序列化
8. 序列化、反序列化
- 序列化
/**
* 使用ObjectOutputStream实现对象的序列化
* 要求:序列化类必须实现Serializable接口
*/
public class MySerialize {
public static void main(String[] args) throws Exception {
// 创建对象流
FileOutputStream fos=new FileOutputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc3.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
// 序列化(写入操作)
Student zhan=new Student("zhan",20);
oos.writeObject(zhan);
oos.flush();
// 关闭
oos.close();
}
}
- 反序列化
/**
* 反序列化的实现
*/
public class MySerialize2 {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc3.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student s=(Student) ois.readObject();
ois.close();
System.out.println(s.toString());
}
}
- 注意事项
- 某个类要想序列化必须实现Serializable接口
- 序列化类中对象属性要求实现Serializable接口
- 序列化版本号ID,保证序列化的类和反序列化的类是同一个类
- 使用transient修饰属性,这个属性就不能序列化
- 静态属性不能序列化
- 序列化多个对象,可以借助集合来实现
9. 字符流
-
Reader 输入字符流
-
Writer 输出字符流
文件字符流
- FileReader
创建文件字符输入流:FileReader fr=new FileReader("path");
- FileWriter
创建文件字符输出流:FileWiter fw=new FileWriter("path");
操作同FileInputStream/FileOutputStream,注意使用字符流不能复制图片或二进制文件,使用字节流可以复制任意文件
10. 字符缓冲流
BufferedReader/BufferedWriter
public class IOTest5 {
public static void main(String[] args) throws Exception{
FileReader fr=new FileReader("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/abc.txt");
BufferedReader br=new BufferedReader(fr);
//第一种方式
// char[] buf=new char[1024];
// int count=0;
// while ((count=br.read(buf))!=-1){
// System.out.println(new String(buf,0,count));
// }
//一行一行读入
String data=null;
while ((data=br.readLine())!=null){
System.out.println(data);
}
br.close();
}
}
BufferedWriter不再赘述
11. 转换流
桥转换流 InputStreamReader / OutputStreamWriter
-
可将字节流转换为字符流
-
可设置字符的编码方式、
FileInputStream fis = new FisInputStream("path");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
12. File类
方法名 | 解释 |
---|---|
createNewFile() | 创建一个新文件 |
mkdir() | 创建一个新目录 |
delete() | 删除文件或空目录 |
exists() | 判断文件或文件夹是否存在 |
getAbsolutePath() | 获取文件的绝对路径 |
getName() | 获取文件名,注意会带文件后缀 |
getParent() | 获取文件/目录所在的目录 |
isDirectory() | 判断是否是目录 |
isFile() | 是否为文件 |
length() | 获取文件的长度 |
listFiles() | 列出目录中的所有内容 |
renameTo() | 修改文件名为 |
deleteOnExit() | jvm退出后删除文件 |
canWrite() | 判断文件是否可写入 |
13. Properties
package cn.pearbook.demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
public class MyProperties {
public static void main(String[] args) throws Exception{
Properties properties=new Properties();
properties.setProperty("username","zhang");
properties.setProperty("age","23");
Set<String> pronames=properties.stringPropertyNames();
for (String pro:pronames
) {
System.out.println(pro+"<-->"+properties.getProperty(pro));
}
//和流有关的方法
//list
PrintWriter printWriter=new PrintWriter("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/prin.txt");
properties.list(printWriter);
printWriter.close();
//store
FileOutputStream fos = new FileOutputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/store.properties");
properties.store(fos,"zhushi");
fos.close();
//load
Properties properties1=new Properties();
FileInputStream fis = new FileInputStream("/Users/richie/IdeaProjects/demo/src/cn/pearbook/demo/store.properties");
properties1.load(fis);
System.out.println(properties1.toString());
}
}
🔚
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现