一、IO 概述

一、

一、IO 概述

1. 什么是 IO

  • I :Input 输入(),从外部设备 → 内存
  • O :Output 输出(),从内存 → 外部设备
  • 作用:读写文件、网络数据、内存数据

2. 流的分类

  1. 按数据方向
    • 输入流(读)
    • 输出流(写)
  2. 按数据单位(最常用)
    • 字节流 :处理一切文件(文本、图片、视频、音频),后缀 Stream
    • 字符流只处理纯文本文件 (.txt/.java/.md),后缀 Reader/Writer
  3. 按功能
    • 节点流(基础流):直接操作数据源
    • 处理流(包装流 / 缓冲流):增强性能,套在基础流外面

3. 四大基类(IO 顶层父类,抽象类

表格

分类 输入(读) 输出(写)
字节流 InputStream OutputStream
字符流 Reader Writer

所有 IO 流子类,都继承以上四个抽象类。


二、字节流(万能流)

1. 文件字节流(基础节点流)

(1)FileInputStream 文件字节输入流(读文件)

常用方法

  • int read():读单个字节 ,返回字节值;读到末尾返回 -1
  • int read(byte[] b):读字节数组 ,返回实际读取长度;末尾 -1
  • close():关闭流(必须执行,释放资源

代码模板(单字节读取)

java

运行

复制代码
FileInputStream fis = new FileInputStream("a.txt");
int b;
while ((b = fis.read()) != -1) {
    System.out.print((char) b);
}
fis.close();

代码模板(字节数组读取,效率更高)

java

运行

复制代码
FileInputStream fis = new FileInputStream("a.txt");
byte[] buf = new byte[1024]; // 缓冲区
int len;
while ((len = fis.read(buf)) != -1) {
    System.out.print(new String(buf, 0, len));
}
fis.close();
(2)FileOutputStream 文件字节输出流(写文件)

构造方法

  • new FileOutputStream("a.txt")覆盖原有内容
  • new FileOutputStream("a.txt", true)追加内容(不覆盖)

常用方法

  • write(int b):写单个字节
  • write(byte[] b):写字节数组
  • write(byte[] b, int off, int len):写数组指定区间
  • close():关闭流

代码模板

java

运行

复制代码
FileOutputStream fos = new FileOutputStream("b.txt");
fos.write("hello io".getBytes());
fos.close();

2. 字节缓冲流(处理流,提速)

底层自带缓冲区,读写效率远高于基础字节流

  • BufferedInputStream 字节缓冲输入
  • BufferedOutputStream 字节缓冲输出

使用方式:包装基础流

java

运行

复制代码
// 读
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
// 写
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));

关闭规则:只关外层缓冲流即可,内层自动关闭

3. 字节流经典案例:文件复制(万能复制)

可复制任意类型文件(图片、视频、文档)

java

运行

复制代码
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("源文件.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("目标.jpg"));

byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
    bos.write(buf, 0, len);
}

bis.close();
bos.close();

三、字符流(只操作纯文本)

1. 为什么有字符流?

字节读中文可能乱码 (一个中文占 2~3 字节),字符流按字符读取,专门解决文本乱码。

2. 文件字符流

FileReader / FileWriter

用法和字节流几乎一致:

  • FileReader:字符输入(读文本)
  • FileWriter:字符输出(写文本)

写文件特点

  • 字符流有缓冲区 ,数据先存在内存缓冲区,必须 flush () 或 close () 才会写入文件
    • flush():刷新缓冲区,流继续可用
    • close():先刷新,再关闭流(流不可再用)

代码示例

java

运行

复制代码
FileWriter fw = new FileWriter("c.txt");
fw.write("中文测试");
fw.flush(); // 刷新
fw.close();

3. 字符缓冲流(重点:带行读写)

  • BufferedReader 字符缓冲输入
  • BufferedWriter 字符缓冲输出

独有常用方法

  1. BufferedReader
    • String readLine()读取一整行 ,读到末尾返回 null(最常用)
  2. BufferedWriter
    • newLine()换行 (跨平台通用,比 \n 兼容好)

逐行读取文本模板

java

运行

复制代码
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
br.close();

四、IO 异常处理(标准写法:try-with-resources)

JDK7+ 推荐 try-with-resources自动关闭流 ,不用手动写 close 格式:try(流对象){ 读写逻辑 }

标准模板(文件复制)

java

运行

复制代码
try (
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("源.txt"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("目标.txt"))
) {
    byte[] buf = new byte[1024];
    int len;
    while ((len = bis.read(buf)) != -1) {
        bos.write(buf, 0, len);
    }
} catch (IOException e) {
    e.printStackTrace();
}

五、转换流(字节 ↔ 字符,解决编码乱码)

作用:指定编码格式(UTF-8 / GBK),解决中文乱码

  1. InputStreamReader字节流 → 字符流(解码)
  2. OutputStreamWriter字符流 → 字节流(编码)

指定编码读取

java

运行

复制代码
// 以 GBK 编码读文件
BufferedReader br = new BufferedReader(
    new InputStreamReader(new FileInputStream("a.txt"), "GBK")
);

六、序列化流(对象读写)

Java 对象 写入文件 / 网络(持久化对象)

1. 两个流

  • ObjectOutputStream序列化(对象 → 文件)
  • ObjectInputStream反序列化(文件 → 对象)

2. 前提条件

实体类必须实现接口:implements Serializable(标记接口,无方法)

3. 关键字

  • transient瞬态修饰 ,被修饰的成员变量不会被序列化

4. 简单流程

  1. 实体类实现 Serializable
  2. ObjectOutputStream → 写对象 writeObject(对象)
  3. ObjectInputStream → 读对象 readObject()

七、IO 核心总结(必背考点)

  1. 字节流:后缀 Stream,万能,所有文件都能操作
  2. 字符流 :后缀 Reader/Writer,只做纯文本,解决中文乱码
  3. 缓冲流 :包装流,提升读写速度,字符缓冲流支持 readLine()/newLine()
  4. 流关闭:try-with-resources 自动关闭,优先使用
  5. 转换流 :用来指定编码,解决乱码
  6. 序列化 :对象持久化,类必须实现 Serializable

八、使用选择口诀

  • 复制图片 / 视频 / 压缩包 → 字节流
  • 读写纯文本、中文多 → 字符流
  • 追求效率 → 一律用缓冲流
  • 有编码乱码 → 用转换流
  • 读写对象 → 序列化

更多

1. 什么是 IO

  • I :Input 输入(),从外部设备 → 内存
  • O :Output 输出(),从内存 → 外部设备
  • 作用:读写文件、网络数据、内存数据

2. 流的分类

  1. 按数据方向
    • 输入流(读)
    • 输出流(写)
  2. 按数据单位(最常用)
    • 字节流 :处理一切文件(文本、图片、视频、音频),后缀 Stream
    • 字符流只处理纯文本文件 (.txt/.java/.md),后缀 Reader/Writer
  3. 按功能
    • 节点流(基础流):直接操作数据源
    • 处理流(包装流 / 缓冲流):增强性能,套在基础流外面

3. 四大基类(IO 顶层父类,抽象类

表格

分类 输入(读) 输出(写)
字节流 InputStream OutputStream
字符流 Reader Writer

所有 IO 流子类,都继承以上四个抽象类。


二、字节流(万能流)

1. 文件字节流(基础节点流)

(1)FileInputStream 文件字节输入流(读文件)

常用方法

  • int read():读单个字节 ,返回字节值;读到末尾返回 -1
  • int read(byte[] b):读字节数组 ,返回实际读取长度;末尾 -1
  • close():关闭流(必须执行,释放资源

代码模板(单字节读取)

java

运行

复制代码
FileInputStream fis = new FileInputStream("a.txt");
int b;
while ((b = fis.read()) != -1) {
    System.out.print((char) b);
}
fis.close();

代码模板(字节数组读取,效率更高)

java

运行

复制代码
FileInputStream fis = new FileInputStream("a.txt");
byte[] buf = new byte[1024]; // 缓冲区
int len;
while ((len = fis.read(buf)) != -1) {
    System.out.print(new String(buf, 0, len));
}
fis.close();
(2)FileOutputStream 文件字节输出流(写文件)

构造方法

  • new FileOutputStream("a.txt")覆盖原有内容
  • new FileOutputStream("a.txt", true)追加内容(不覆盖)

常用方法

  • write(int b):写单个字节
  • write(byte[] b):写字节数组
  • write(byte[] b, int off, int len):写数组指定区间
  • close():关闭流

代码模板

java

运行

复制代码
FileOutputStream fos = new FileOutputStream("b.txt");
fos.write("hello io".getBytes());
fos.close();

2. 字节缓冲流(处理流,提速)

底层自带缓冲区,读写效率远高于基础字节流

  • BufferedInputStream 字节缓冲输入
  • BufferedOutputStream 字节缓冲输出

使用方式:包装基础流

java

运行

复制代码
// 读
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
// 写
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));

关闭规则:只关外层缓冲流即可,内层自动关闭

3. 字节流经典案例:文件复制(万能复制)

可复制任意类型文件(图片、视频、文档)

java

运行

复制代码
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("源文件.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("目标.jpg"));

byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
    bos.write(buf, 0, len);
}

bis.close();
bos.close();

三、字符流(只操作纯文本)

1. 为什么有字符流?

字节读中文可能乱码 (一个中文占 2~3 字节),字符流按字符读取,专门解决文本乱码。

2. 文件字符流

FileReader / FileWriter

用法和字节流几乎一致:

  • FileReader:字符输入(读文本)
  • FileWriter:字符输出(写文本)

写文件特点

  • 字符流有缓冲区 ,数据先存在内存缓冲区,必须 flush () 或 close () 才会写入文件
    • flush():刷新缓冲区,流继续可用
    • close():先刷新,再关闭流(流不可再用)

代码示例

java

运行

复制代码
FileWriter fw = new FileWriter("c.txt");
fw.write("中文测试");
fw.flush(); // 刷新
fw.close();

3. 字符缓冲流(重点:带行读写)

  • BufferedReader 字符缓冲输入
  • BufferedWriter 字符缓冲输出

独有常用方法

  1. BufferedReader
    • String readLine()读取一整行 ,读到末尾返回 null(最常用)
  2. BufferedWriter
    • newLine()换行 (跨平台通用,比 \n 兼容好)

逐行读取文本模板

java

运行

复制代码
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
br.close();

四、IO 异常处理(标准写法:try-with-resources)

JDK7+ 推荐 try-with-resources自动关闭流 ,不用手动写 close 格式:try(流对象){ 读写逻辑 }

标准模板(文件复制)

java

运行

复制代码
try (
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("源.txt"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("目标.txt"))
) {
    byte[] buf = new byte[1024];
    int len;
    while ((len = bis.read(buf)) != -1) {
        bos.write(buf, 0, len);
    }
} catch (IOException e) {
    e.printStackTrace();
}

五、转换流(字节 ↔ 字符,解决编码乱码)

作用:指定编码格式(UTF-8 / GBK),解决中文乱码

  1. InputStreamReader字节流 → 字符流(解码)
  2. OutputStreamWriter字符流 → 字节流(编码)

指定编码读取

java

运行

复制代码
// 以 GBK 编码读文件
BufferedReader br = new BufferedReader(
    new InputStreamReader(new FileInputStream("a.txt"), "GBK")
);

六、序列化流(对象读写)

Java 对象 写入文件 / 网络(持久化对象)

1. 两个流

  • ObjectOutputStream序列化(对象 → 文件)
  • ObjectInputStream反序列化(文件 → 对象)

2. 前提条件

实体类必须实现接口:implements Serializable(标记接口,无方法)

3. 关键字

  • transient瞬态修饰 ,被修饰的成员变量不会被序列化

4. 简单流程

  1. 实体类实现 Serializable
  2. ObjectOutputStream → 写对象 writeObject(对象)
  3. ObjectInputStream → 读对象 readObject()

七、IO 核心总结(必背考点)

  1. 字节流:后缀 Stream,万能,所有文件都能操作
  2. 字符流 :后缀 Reader/Writer,只做纯文本,解决中文乱码
  3. 缓冲流 :包装流,提升读写速度,字符缓冲流支持 readLine()/newLine()
  4. 流关闭:try-with-resources 自动关闭,优先使用
  5. 转换流 :用来指定编码,解决乱码
  6. 序列化 :对象持久化,类必须实现 Serializable

八、使用选择口诀

  • 复制图片 / 视频 / 压缩包 → 字节流
  • 读写纯文本、中文多 → 字符流
  • 追求效率 → 一律用缓冲流
  • 有编码乱码 → 用转换流
  • 读写对象 → 序列化流

快速

编程

视频生成

图像生成

帮我写作

翻译

更多

相关推荐
兵慌码乱4 小时前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
hboot5 小时前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle
顾林海10 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱13 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽18 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码18 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱1 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵1 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio1 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663671 天前
使用 Python 从零创建 Word 文档
python