1.1 本章学习目标与重点
在Java 的 IO 体系中,字节流和字符流可以说是最基础、也最容易混淆的一组概念。很多初学者都会疑惑:到底什么时候该用字节流,什么时候该用字符流?

其实答案并不复杂:先理解两者的核心区别,再结合具体开发场景选择即可。像图片、视频这类二进制文件,优先使用字节流;如果是文本文件,字符流通常更方便,因为它自带编码处理能力。
另外,缓冲流是提升 IO 性能的关键,尤其在处理大文件时,是否使用缓冲流的效率差异会非常明显。转换流则是解决乱码问题的重要工具,当文件编码与系统默认编码不一致时,它就能发挥作用。
⚠️ 需要重点掌握的是流的嵌套使用和资源释放的标准写法,这两部分不仅是面试高频考点,在实际开发中也非常容易出错。
1.2 字节流与字符流的核心差异(七千字以上内容展开)
1.2.1 基本概念与设计初衷
字节流以byte为基本单位进行数据传输,这意味着它可以处理所有类型的文件,包括图片、视频、音频和文本文件。字符流则以char为单位,主要为文本文件设计,底层会自动完成字符编码转换。
从类的继承体系来看,字节流的基类是InputStream和OutputStream,字符流的基类是Reader和Writer。它们都属于抽象类,实际开发中通常使用具体子类,比如FileInputStream、FileWriter等。
✅ 一句话总结:非文本文件优先使用字节流,文本文件优先考虑字符流。
1.2.2 代码实操:字节流读写文本文件
下面来看字节流如何读取文本文件,步骤如下:
① 创建FileInputStream对象,关联目标文件test.txt
② 定义byte数组作为缓冲区,减少 IO 次数
③ 循环读取数据并转换为字符串输出
④ 关闭流资源,释放文件句柄
import ja va.io.FileInputStream;
import ja va.io.IOException;
public class ByteStreamDemo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
// 1. 关联文件路径
fis = new FileInputStream("test.txt");
// 2. 定义缓冲区,大小为1024字节(1KB)
byte[] buffer = new byte[1024];
int len; // 记录每次读取的有效字节数
// 3. 循环读取数据
while ((len = fis.read(buffer)) != -1) {
// 将字节数组转换为字符串
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭流资源
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
⚠️ 一个常见问题:如果使用字节流读取文本文件,而文件编码是UTF-8、系统默认编码却是GBK,就很容易出现乱码。这时通常需要字符流或转换流来解决。
1.2.3 代码实操:字符流读写文本文件
字符流的优势在于它能自动处理字符编码,默认使用系统编码,当然也可以手动指定。下面使用FileReader和FileWriter演示文本文件复制:
① 创建FileReader对象读取源文件,创建FileWriter对象写入目标文件
② 定义char数组作为缓冲区
③ 循环读取源文件数据并写入目标文件
④ 关闭流资源,先关写入流,再关读取流
import ja va.io.FileReader;
import ja va.io.FileWriter;
import ja va.io.IOException;
public class CharStreamDemo {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
// 1. 关联源文件和目标文件
fr = new FileReader("source.txt");
fw = new FileWriter("target.txt");
// 2. 定义字符缓冲区
char[] buffer = new char[1024];
int len;
// 3. 循环读写
while ((len = fr.read(buffer)) != -1) {
fw.write(buffer, 0, len);
// 刷新缓冲区,避免数据滞留
fw.flush();
}
System.out.println("✅ 文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭流资源,后开先关
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
✅ 对比来看,字符流在读写文本文件时无需手动处理编码转换,代码更简洁,乱码问题也会少很多。
1.2.4 字节流与字符流的性能对比
如果不使用缓冲机制,字节流和字符流的读写效率其实差别不大。但在处理大文件时,两者都需要配合缓冲流,才能真正提升性能。
缓冲流的原理也很简单:在内存中开辟一块缓冲区,一次性读入或写出更多数据,从而减少与磁盘交互的次数。字节缓冲流对应BufferedInputStream和BufferedOutputStream,字符缓冲流对应BufferedReader和BufferedWriter。
下面做一个简单测试:分别使用普通字节流和缓冲字节流读取一个100MB的视频文件,观察耗时差异。
import ja va.io.BufferedInputStream;
import ja va.io.FileInputStream;
import ja va.io.IOException;
public class BufferedStreamTest {
public static void main(String[] args) {
long start = System.currentTimeMillis();
readWithBuffer("large_video.mp4");
long end = System.currentTimeMillis();
System.out.println("缓冲流耗时:" + (end - start) + "ms");
start = System.currentTimeMillis();
readWithoutBuffer("large_video.mp4");
end = System.currentTimeMillis();
System.out.println("普通流耗时:" + (end - start) + "ms");
}
// 使用缓冲字节流读取文件
private static void readWithBuffer(String path) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path))) {
byte[] buffer = new byte[1024];
while (bis.read(buffer) != -1) {
// 读取数据,不做输出
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用普通字节流读取文件
private static void readWithoutBuffer(String path) {
try (FileInputStream fis = new FileInputStream(path)) {
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
// 读取数据,不做输出
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果(仅供参考):
缓冲流耗时:120ms 普通流耗时:850ms
✅ 差距非常明显:缓冲流的效率提升接近7倍。对于大文件处理来说,缓冲流几乎是必备方案。
1.3 转换流:解决文件编码乱码问题
1.3.1 转换流的作用
转换流的核心作用是在字节流和字符流之间进行转换,同时支持指定字符编码格式,从而解决文本文件读写时的乱码问题。核心类有两个:InputStreamReader和OutputStreamWriter。
InputStreamReader:将字节输入流转换为字符输入流。
OutputStreamWriter:将字符输出流转换为字节输出流。
1.3.2 代码实操:指定编码读取文件
举个例子:如果系统默认编码是GBK,但我们要读取一个UTF-8编码的文件,直接使用FileReader通常会出现乱码。这时就可以借助InputStreamReader:
import ja va.io.FileInputStream;
import ja va.io.IOException;
import ja va.io.InputStreamReader;
public class ConvertStreamDemo {
public static void main(String[] args) {
try (InputStreamReader isr = new InputStreamReader(
new FileInputStream("utf8_file.txt"), "UTF-8")) {
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
⚠️ 关键点:指定的编码格式必须与文件的实际编码保持一致,否则依然会乱码。常见编码格式包括UTF-8、GBK、GB2312、ISO-8859-1等。
1.4 IO流资源释放的标准写法
1.4.1 JDK7之前的写法:try-catch-finally
在JDK7之前,我们必须在finally块中手动关闭流资源,还要判断流对象是否为null,以避免空指针异常。这种写法虽然稳妥,但代码比较冗长。
1.4.2 JDK7及之后的写法:try-with-resources
JDK7 引入了try-with-resources语法糖,它可以自动关闭实现了AutoCloseable接口的资源,不再需要手动编写finally块。这样不仅代码更简洁,可读性也更高,是当前推荐的标准写法。
import ja va.io.BufferedReader;
import ja va.io.FileReader;
import ja va.io.IOException;
public class TryWithResourcesDemo {
public static void main(String[] args) {
// 将流对象声明在try的括号中,自动关闭
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
// 按行读取文本文件
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
✅ 结论很明确:在 JDK7 及以上版本中,优先使用try-with-resources,可以显著精简资源释放代码。
1.5 实战案例:文件夹批量复制工具
1.5.1 需求分析
实现一个工具类,用于复制指定文件夹下的所有文件和子文件夹,涵盖文本、图片、视频等多种类型。具体要求如下:
- 支持大文件复制,使用缓冲流提升效率
- 自动创建目标文件夹,避免路径不存在导致异常
- 处理复制过程中的 IO 异常,并给出友好提示
1.5.2 代码实现
import ja va.io.*;
public class FolderCopyUtil {
public static void main(String[] args) {
String sourcePath = "D:\source_folder";
String targetPath = "D:\target_folder";
try {
copyFolder(sourcePath, targetPath);
System.out.println("✅ 文件夹复制成功!");
} catch (IOException e) {
System.out.println("❌ 文件夹复制失败:" + e.getMessage());
e.printStackTrace();
}
}
/**
* 复制文件夹
* @param sourcePath 源文件夹路径
* @param targetPath 目标文件夹路径
* @throws IOException IO异常
*/
public static void copyFolder(String sourcePath, String targetPath) throws IOException {
File sourceFile = new File(sourcePath);
File targetFile = new File(targetPath);
// 1. 如果源文件不是文件夹,直接复制文件
if (!sourceFile.isDirectory()) {
copyFile(sourceFile, targetFile);
return;
}
// 2. 创建目标文件夹
if (!targetFile.exists()) {
boolean mkdirsSuccess = targetFile.mkdirs();
if (!mkdirsSuccess) {
throw new IOException("创建目标文件夹失败:" + targetPath);
}
}
// 3. 获取源文件夹下的所有文件和子文件夹
File[] files = sourceFile.listFiles();
if (files == null) {
return;
}
// 4. 循环复制每个文件和子文件夹
for (File file : files) {
String newSourcePath = file.getAbsolutePath();
String newTargetPath = targetPath + File.separator + file.getName();
copyFolder(newSourcePath, newTargetPath);
}
}
/**
* 复制单个文件
* @param sourceFile 源文件
* @param targetFile 目标文件
* @throws IOException IO异常
*/
public static void copyFile(File sourceFile, File targetFile) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile))) {
byte[] buffer = new byte[1024 * 8]; // 8KB缓冲区
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
bos.flush();
}
}
}
}
1.5.3 案例测试与总结
① 创建一个测试文件夹,放入文本、图片、视频等多种类型文件和子文件夹。
② 运行代码,指定源文件夹和目标文件夹路径。
③ 检查目标文件夹,确认所有文件和子文件夹都已成功复制。
✅ 这个工具类将字节流、缓冲流和递归遍历结合在一起,是实际开发中非常实用的功能。通过它,可以深入理解 IO 流的嵌套使用和文件夹递归遍历技巧。
1.6 本章总结
- 字节流适合处理所有类型文件,字符流更适合文本文件,应根据场景灵活选择。
- 缓冲流能够显著提升 IO 效率,尤其在处理大文件时几乎必不可少。
- 转换流通过指定编码格式来解决乱码问题。
- JDK7 及以上版本优先使用
try-with-resources自动释放资源。 - 文件夹复制案例是 IO 流核心知识的综合应用,对提升实战能力非常有帮助。
