在Java编程中,输入输出(Input/Output,简称IO)是程序与外部世界交互的核心机制。无论是读取配置文件、处理用户输入、操作数据库,还是与网络服务通信,都离不开对数据流的处理。Java提供了一套完善的IO体系,涵盖了从简单文件读写到复杂数据流操作的各种场景。
本文将系统讲解Java IO流的核心概念、体系结构及实战用法,通过大量代码示例帮助开发者掌握文件与数据流的处理技巧,解决实际开发中的IO问题。
一、Java IO流基础:核心概念与体系结构
在深入代码之前,我们需要先理解IO流的本质及其在Java中的组织方式。
1. 什么是IO流?
流(Stream) 是Java中处理数据传输的抽象概念,它将数据的传输过程抽象为"流"的形式——数据像水流一样从一个地方流向另一个地方。
- 输入流(Input Stream):数据从外部源(如文件、网络、键盘)流向程序。
- 输出流(Output Stream):数据从程序流向外部目标(如文件、网络、显示器)。
流的特性:
- 顺序读写:流只能按顺序读取或写入,不能随机访问(某些特殊流除外,如
RandomAccessFile
)。 - 单向流动:输入流和输出流是单向的,一个流要么用于输入,要么用于输出。
- 可关闭性:流操作涉及系统资源(如文件句柄),使用后必须关闭,否则会导致资源泄漏。
2. IO流的分类
Java IO流体系庞大,可按不同维度分类:
按数据单位分类
- 字节流:以字节(8位)为单位处理数据,适用于所有类型的文件(文本、图片、音频等)。
- 基类:
InputStream
(输入)、OutputStream
(输出)。
- 基类:
- 字符流:以字符(16位Unicode)为单位处理数据,仅适用于文本文件,会涉及字符编码(如UTF-8、GBK)。
- 基类:
Reader
(输入)、Writer
(输出)。
- 基类:
按流的角色分类
- 节点流(Node Stream):直接与数据源(如文件、内存)连接,是IO操作的底层流。
- 示例:
FileInputStream
、FileReader
。
- 示例:
- 处理流(Processing Stream):包裹在节点流或其他处理流之上,用于增强功能(如缓冲、转换、过滤)。
- 示例:
BufferedInputStream
、InputStreamReader
、ObjectInputStream
。
- 示例:
按功能分类
- 文件流:直接操作文件的流(如
FileInputStream
)。 - 缓冲流:提供缓冲功能,提升读写效率(如
BufferedReader
)。 - 转换流:实现字节流与字符流的转换(如
InputStreamReader
)。 - 数据流:读写基本数据类型(如
DataInputStream
)。 - 对象流:实现对象的序列化与反序列化(如
ObjectInputStream
)。 - 打印流:方便输出数据(如
PrintStream
、PrintWriter
)。
3. IO流体系结构概览
Java IO流的类主要位于java.io
包下,核心类继承关系如下:
// 字节流体系
InputStream
├─ FileInputStream(文件输入流)
├─ BufferedInputStream(缓冲输入流)
├─ DataInputStream(数据输入流)
├─ ObjectInputStream(对象输入流)
├─ ByteArrayInputStream(字节数组输入流)
└─ ...
OutputStream
├─ FileOutputStream(文件输出流)
├─ BufferedOutputStream(缓冲输出流)
├─ DataOutputStream(数据输出流)
├─ ObjectOutputStream(对象输出流)
├─ ByteArrayOutputStream(字节数组输出流)
└─ ...
// 字符流体系
Reader
├─ FileReader(文件读取器)
├─ BufferedReader(缓冲读取器)
├─ InputStreamReader(字节转字符输入流)
├─ CharArrayReader(字符数组读取器)
└─ ...
Writer
├─ FileWriter(文件写入器)
├─ BufferedWriter(缓冲写入器)
├─ OutputStreamWriter(字符转字节输出流)
├─ CharArrayWriter(字符数组写入器)
└─ ...
理解这个体系结构有助于我们在不同场景下选择合适的流。
二、字节流:处理二进制数据
字节流是Java IO的基础,可处理所有类型的数据(文本、图片、视频等)。本节重点讲解常用字节流的使用方法。
1. 节点流:FileInputStream与FileOutputStream
FileInputStream
和FileOutputStream
是直接操作文件的字节流,属于节点流。
读取文件:FileInputStream
FileInputStream
用于从文件中读取字节数据,常用方法:
int read()
:读取一个字节,返回字节值(0-255),若到达文件末尾返回-1。int read(byte[] b)
:读取字节到数组b
中,返回实际读取的字节数,若到达末尾返回-1。void close()
:关闭流,释放资源。
示例1:使用FileInputStream读取文件
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args) {
// 定义文件路径
String filePath = "demo.txt";
FileInputStream fis = null;
try {
// 创建文件输入流
fis = new FileInputStream(filePath);
// 方式1:逐个字节读取(效率低,适合小文件)
int data;
System.out.println("逐个字节读取:");
while ((data = fis.read()) != -1) {
// 输出字节对应的字符(仅适用于文本文件)
System.out.print((char) data);
}
// 重置文件指针(需重新创建流,因为read()会移动指针到末尾)
fis.close();
fis = new FileInputStream(filePath);
// 方式2:按字节数组读取(效率高,推荐)
byte[] buffer = new byte[1024]; // 缓冲区大小,通常为1024的倍数
int len; // 实际读取的字节数
System.out.println("\n按字节数组读取:");
while ((len = fis.read(buffer)) != -1) {
// 将字节数组转换为字符串(注意编码,默认使用平台编码)
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 确保流被关闭
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
写入文件:FileOutputStream
FileOutputStream
用于向文件中写入字节数据,常用方法:
void write(int b)
:写入一个字节。void write(byte[] b)
:写入字节数组b
的所有内容。void write(byte[] b, int off, int len)
:写入字节数组b
中从off
开始的len
个字节。void close()
:关闭流。
示例2:使用FileOutputStream写入文件
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) {
String filePath = "output.txt";
FileOutputStream fos = null;
try {
// 创建文件输出流:
// - 若文件不存在,会自动创建
// - 第二个参数为true时,追加内容;默认false,覆盖文件
fos = new FileOutputStream(filePath, true);
// 写入字符串(需先转换为字节数组)
String content = "Hello, FileOutputStream!\n";
byte[] data = content.getBytes(); // 注意:默认使用平台编码,建议指定编码
// 方式1:写入整个字节数组
fos.write(data);
// 方式2:写入部分字节
String part = "Partial content";
byte[] partData = part.getBytes();
fos.write(partData, 0, 6); // 写入前6个字节:"Partial"
System.out.println("写入完成!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:getBytes()
方法默认使用平台编码(如Windows的GBK),可能导致跨平台时出现乱码。建议显式指定编码,如content.getBytes("UTF-8")
。
2. 处理流:缓冲流(BufferedInputStream/BufferedOutputStream)
缓冲流通过在内存中创建缓冲区(默认8KB),减少对磁盘的IO操作次数,显著提升读写效率。使用时需包裹一个节点流(如FileInputStream
)。
示例3:使用缓冲流复制文件(高效)
import java.io.*;
public class BufferedStreamDemo {
public static void main(String[] args) {
// 源文件和目标文件路径
String sourcePath = "source.jpg"; // 可以是任何类型的文件(图片、视频等)
String destPath = "copy.jpg";
// 声明流对象
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建节点流
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(destPath);
// 创建缓冲流(包裹节点流)
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 缓冲数组(大小可根据文件类型调整,通常4KB-8KB)
byte[] buffer = new byte[8192];
int len;
long start = System.currentTimeMillis();
// 读写数据
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
// 强制刷新缓冲区(缓冲流会自动刷新,但关闭前建议手动刷新)
bos.flush();
long end = System.currentTimeMillis();
System.out.println("复制完成!耗时:" + (end - start) + "ms");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流(只需关闭外层的缓冲流,会自动关闭内层节点流)
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
为什么缓冲流效率更高?
普通节点流每次读写都会直接操作磁盘,而缓冲流先将数据读入内存缓冲区,当缓冲区满或调用flush()
时才一次性写入磁盘,减少了磁盘IO次数(磁盘IO是非常耗时的操作)。
3. 字节数组流:ByteArrayInputStream/ByteArrayOutputStream
字节数组流以内存中的字节数组为数据源或目标,用于在内存中处理数据(如临时数据转换),无需操作磁盘。
示例4:使用字节数组流在内存中处理数据
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayStreamDemo {
public static void main(String[] args) {
// 原始数据
String data = "Hello, ByteArrayStream!";
byte[] source = data.getBytes();
// ByteArrayInputStream:从字节数组读取数据
try (ByteArrayInputStream bais = new ByteArrayInputStream(source)) {
byte[] buffer = new byte[10];
int len;
System.out.println("读取字节数组流:");
while ((len = bais.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
// ByteArrayOutputStream:向字节数组写入数据(内部自动扩容)
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
String part1 = "Hello, ";
String part2 = "World!";
baos.write(part1.getBytes());
baos.write(part2.getBytes());
// 获取最终的字节数组
byte[] result = baos.toByteArray();
System.out.println("\n字节数组流写入结果:" + new String(result));
} catch (IOException e) {
e.printStackTrace();
}
}
}
应用场景:
- 数据格式转换(如将对象序列化为字节数组)。
- 临时存储数据,避免频繁创建临时文件。
- 网络传输中数据的临时缓存。
三、字符流:处理文本数据
字符流专门用于处理文本数据,自动处理字符编码转换,避免字节流处理文本时可能出现的乱码问题。
1. 转换流:InputStreamReader与OutputStreamWriter
字节流与字符流的桥梁,用于将字节流转换为字符流,并指定字符编码(如UTF-8)。
InputStreamReader
:将字节输入流转换为字符输入流,需传入字节流和编码。OutputStreamWriter
:将字符输出流转换为字节输出流,需传入字节流和编码。
示例5:使用转换流处理文本文件(指定编码)
import java.io.*;
public class ConvertStreamDemo {
public static void main(String[] args) {
String filePath = "utf8_file.txt";
// 写入UTF-8编码的文本文件
try (
// 字节流
FileOutputStream fos = new FileOutputStream(filePath);
// 转换流:指定编码为UTF-8
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
) {
osw.write("Hello, 世界!"); // 可以直接写入中文字符
osw.write("\n这是UTF-8编码的文本文件");
System.out.println("写入完成!");
} catch (IOException e) {
e.printStackTrace();
}
// 读取UTF-8编码的文本文件
try (
FileInputStream fis = new FileInputStream(filePath);
// 转换流:指定编码为UTF-8(必须与写入时一致,否则乱码)
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
) {
char[] buffer = new char[1024];
int len;
System.out.println("读取结果:");
while ((len = isr.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
为什么需要指定编码?
文本在存储时以字节形式存在(如UTF-8中一个汉字占3字节,GBK中占2字节)。读取时必须使用与写入时相同的编码规则,否则会出现乱码。转换流的核心作用就是明确指定编码,避免依赖平台默认编码。
2. 文件字符流:FileReader与FileWriter
FileReader
和FileWriter
是简化版的转换流,底层默认使用平台编码,等价于:
FileReader(file)
≈new InputStreamReader(new FileInputStream(file), Charset.defaultCharset())
FileWriter(file)
≈new OutputStreamWriter(new FileOutputStream(file), Charset.defaultCharset())
示例6:使用FileReader和FileWriter读写文本
import java.io.*;
public class FileReaderWriterDemo {
public static void main(String[] args) {
String source = "source.txt";
String dest = "dest.txt";
// 复制文本文件(使用FileReader和FileWriter)
try (
FileReader fr = new FileReader(source);
FileWriter fw = new FileWriter(dest);
) {
char[] buffer = new char[1024];
int len;
while ((len = fr.read(buffer)) != -1) {
fw.write(buffer, 0, len);
}
System.out.println("文本复制完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:由于FileReader/FileWriter
依赖平台默认编码,可能导致跨平台时出现乱码,推荐优先使用转换流并显式指定编码(如UTF-8)。
3. 缓冲字符流:BufferedReader与BufferedWriter
缓冲字符流在字符流基础上增加缓冲功能,同时提供了更便捷的方法(如按行读取)。
BufferedReader
:提供readLine()
方法,一次读取一行文本。BufferedWriter
:提供newLine()
方法,写入平台无关的换行符。
示例7:使用缓冲字符流按行读写文本
import java.io.*;
public class BufferedCharStreamDemo {
public static void main(String[] args) {
String inputFile = "poem.txt";
String outputFile = "uppercase_poem.txt";
// 读取文本并转换为大写后写入新文件
try (
// 字节流 → 转换流(指定UTF-8) → 缓冲流
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(inputFile), "UTF-8")
);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")
);
) {
String line;
// 按行读取(readLine()返回null表示到达文件末尾)
while ((line = br.readLine()) != null) {
// 转换为大写
String upperLine = line.toUpperCase();
// 写入一行
bw.write(upperLine);
// 写入换行符(跨平台兼容)
bw.newLine();
}
System.out.println("处理完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
优势总结:
- 缓冲提高效率:减少IO操作次数。
- 按行读写:
readLine()
和newLine()
简化文本处理。 - 编码可控:结合转换流可指定编码,避免乱码。
4. 打印流:PrintWriter
PrintWriter
是字符流的一种,提供了便捷的打印方法(如print()
、println()
),支持自动刷新,常用于输出文本数据。
示例8:使用PrintWriter写入格式化文本
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String filePath = "report.txt";
// 创建PrintWriter,指定编码为UTF-8,且自动刷新(当调用println()时)
try (PrintWriter pw = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"),
true // 自动刷新
)) {
// 写入普通文本
pw.println("=== 系统报告 ===");
// 写入格式化文本(类似System.out.printf())
pw.printf("当前时间:%tF %<tT%n", System.currentTimeMillis());
pw.printf("用户数:%d,在线率:%.2f%%%n", 1580, 67.89);
// 写入对象(调用toString()方法)
pw.println("系统信息:" + new SystemInfo("Linux", "JDK 17"));
System.out.println("报告生成完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 自定义类
static class SystemInfo {
private String os;
private String jdk;
public SystemInfo(String os, String jdk) {
this.os = os;
this.jdk = jdk;
}
@Override
public String toString() {
return "OS: " + os + ", JDK: " + jdk;
}
}
}
PrintWriter
的优势:
- 支持多种数据类型的打印(字符串、数字、对象等)。
- 提供格式化输出(
printf()
)。 - 可设置自动刷新,无需手动调用
flush()
。
四、数据流与对象流:处理基本类型与对象
除了字节和字符,Java还提供了专门处理基本数据类型和对象的流。
1. 数据流:DataInputStream与DataOutputStream
数据流用于读写Java基本数据类型(如int
、double
、boolean
等),保持数据的原始类型信息。
示例9:使用数据流读写基本数据类型
import java.io.*;
public class DataStreamDemo {
public static void main(String[] args) {
String filePath = "data.dat";
// 写入基本数据类型
try (
FileOutputStream fos = new FileOutputStream(filePath);
DataOutputStream dos = new DataOutputStream(fos);
) {
dos.writeInt(100); // 写入int
dos.writeDouble(3.14159); // 写入double
dos.writeBoolean(true); // 写入boolean
dos.writeUTF("Hello, DataStream"); // 写入UTF-8字符串
System.out.println("数据写入完成!");
} catch (IOException e) {
e.printStackTrace();
}
// 读取基本数据类型(必须与写入顺序一致)
try (
FileInputStream fis = new FileInputStream(filePath);
DataInputStream dis = new DataInputStream(fis);
) {
int num = dis.readInt();
double pi = dis.readDouble();
boolean flag = dis.readBoolean();
String str = dis.readUTF();
System.out.println("读取结果:");
System.out.println("int: " + num);
System.out.println("double: " + pi);
System.out.println("boolean: " + flag);
System.out.println("String: " + str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:
- 读取顺序必须与写入顺序完全一致,否则会读取错误的数据。
writeUTF()
和readUTF()
采用改良的UTF-8编码,与普通字符串的编码方式不同,只能配合使用。
2. 对象流:ObjectInputStream与ObjectOutputStream
对象流用于实现对象的序列化(将对象转换为字节序列)和反序列化(将字节序列恢复为对象),是Java中对象持久化和网络传输的基础。
序列化的条件
一个对象要能被序列化,必须满足:
- 类实现
java.io.Serializable
接口(标记接口,无方法需实现)。 - 类的所有非瞬态(
transient
)成员变量也必须可序列化(或为基本类型)。
示例10:对象的序列化与反序列化
import java.io.*;
import java.util.Date;
// 可序列化的类(实现Serializable接口)
class User implements Serializable {
// 序列化版本号(建议显式声明,避免类结构变化导致反序列化失败)
private static final long serialVersionUID = 1L;
private String username;
private transient String password; // transient修饰的字段不会被序列化
private int age;
private Date registerTime;
public User(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
this.registerTime = new Date();
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' + // 反序列化后为null
", age=" + age +
", registerTime=" + registerTime +
'}';
}
}
public class ObjectStreamDemo {
public static void main(String[] args) {
String filePath = "user.dat";
// 创建对象
User user = new User("zhangsan", "123456", 25);
System.out.println("序列化前:" + user);
// 序列化:将对象写入文件
try (
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
) {
oos.writeObject(user);
System.out.println("对象序列化完成!");
} catch (IOException e) {
e.printStackTrace();
}
// 反序列化:从文件恢复对象
try (
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fis);
) {
User deserializedUser = (User) ois.readObject();
System.out.println("反序列化后:" + deserializedUser);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
序列化的注意事项
-
serialVersionUID的作用:
类的serialVersionUID
用于标识类的版本。当类结构发生变化(如新增字段)时,若未显式声明serialVersionUID
,Java会自动生成一个新的ID,导致旧版本序列化的对象无法反序列化。建议显式声明serialVersionUID
。 -
transient关键字:
被transient
修饰的字段不会被序列化,反序列化后该字段为默认值(如null
、0
)。适用于敏感信息(如密码)或不需要持久化的临时数据。 -
静态变量不参与序列化:
序列化保存的是对象的状态,而静态变量属于类,不被序列化。 -
继承与序列化:
- 若父类实现
Serializable
,子类自动可序列化。 - 若父类未实现
Serializable
,子类实现了,则父类的成员变量不会被序列化(需父类有默认构造方法,否则反序列化失败)。
- 若父类实现
五、Java NIO:新IO模型
Java 1.4引入了NIO(New IO),提供了与传统IO不同的编程模型,基于通道(Channel)和缓冲区(Buffer),支持非阻塞IO,更适合高并发场景。
1. NIO核心组件
- 缓冲区(Buffer):数据容器,用于存储数据(如
ByteBuffer
、CharBuffer
)。 - 通道(Channel):双向数据通道,可读写数据(如
FileChannel
、SocketChannel
)。 - 选择器(Selector):用于监听多个通道的事件(如可读、可写),支持非阻塞IO。
2. 使用NIO读写文件
NIO的FileChannel
和ByteBuffer
是处理文件的核心类,相比传统IO流,NIO在处理大文件时更高效。
示例11:使用NIO读取文件
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOFileReadDemo {
public static void main(String[] args) {
String filePath = "nio_demo.txt";
try (
FileInputStream fis = new FileInputStream(filePath);
// 获取文件通道
FileChannel channel = fis.getChannel();
) {
// 创建缓冲区(容量为1024字节)
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 从通道读取数据到缓冲区
int bytesRead;
System.out.println("读取文件内容:");
while ((bytesRead = channel.read(buffer)) != -1) {
// 切换缓冲区为读模式(limit = position,position = 0)
buffer.flip();
// 读取缓冲区数据
byte[] bytes = new byte[bytesRead];
buffer.get(bytes);
System.out.print(new String(bytes, "UTF-8"));
// 清空缓冲区,准备下次写入(position = 0,limit = capacity)
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例12:使用NIO写入文件
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOFileWriteDemo {
public static void main(String[] args) {
String filePath = "nio_output.txt";
String content = "Hello, Java NIO!\n这是NIO写入的文本。";
try (
FileOutputStream fos = new FileOutputStream(filePath);
FileChannel channel = fos.getChannel();
) {
// 将字符串转换为字节数组
byte[] bytes = content.getBytes("UTF-8");
// 创建缓冲区并写入数据
ByteBuffer buffer = ByteBuffer.wrap(bytes); // 包装字节数组
// 从缓冲区写入通道
channel.write(buffer);
System.out.println("NIO写入完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例13:使用NIO复制文件(高效)
import java.io.*;
import java.nio.channels.FileChannel;
public class NIOFileCopyDemo {
public static void main(String[] args) {
String source = "large_file.iso";
String dest = "large_file_copy.iso";
try (
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileChannel sourceChannel = fis.getChannel();
FileChannel destChannel = fos.getChannel();
) {
long start = System.currentTimeMillis();
// 直接传输通道数据(高效,底层可能使用操作系统的零拷贝机制)
sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
long end = System.currentTimeMillis();
System.out.println("复制完成!耗时:" + (end - start) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
}
NIO的优势:
- 通道双向性:通道可同时读写,而流是单向的。
- 缓冲区操作:数据读写必须经过缓冲区,便于控制数据。
- 高效文件传输:
transferTo()
和transferFrom()
支持零拷贝,适合大文件传输。 - 非阻塞IO:结合选择器(Selector)可实现单线程管理多个通道,适合高并发网络编程。
3. Java 7+ NIO.2:Files与Path
Java 7引入了NIO.2(java.nio.file
包),提供了更简洁的文件操作API,核心类包括Path
(路径)和Files
(文件工具类)。
示例14:使用NIO.2操作文件和目录
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.List;
public class NIO2Demo {
public static void main(String[] args) {
// 创建Path对象(表示文件路径)
Path filePath = Paths.get("nio2_demo.txt");
Path dirPath = Paths.get("demo_dir");
try {
// 1. 创建目录(如果不存在)
if (Files.notExists(dirPath)) {
Files.createDirectory(dirPath);
System.out.println("目录创建成功:" + dirPath);
}
// 2. 写入文件(一行文本)
String content = "Hello, NIO.2!";
Files.write(filePath, content.getBytes(StandardCharsets.UTF_8));
System.out.println("文件写入成功:" + filePath);
// 3. 读取文件(所有行)
List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
System.out.println("文件内容:");
for (String line : lines) {
System.out.println(line);
}
// 4. 复制文件
Path destPath = dirPath.resolve("copied_demo.txt"); // 目标路径
Files.copy(filePath, destPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件复制成功:" + destPath);
// 5. 移动文件(重命名)
Path renamedPath = dirPath.resolve("renamed_demo.txt");
Files.move(destPath, renamedPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件移动成功:" + renamedPath);
// 6. 删除文件
Files.deleteIfExists(renamedPath);
System.out.println("文件删除成功:" + renamedPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
NIO.2的便捷方法:
Files.readAllLines()
:一次性读取所有行(适合小文件)。Files.write()
:简化文件写入。Files.copy()
/Files.move()
:便捷的文件复制和移动。Files.createDirectory()
:创建目录。Files.deleteIfExists()
:删除文件(不存在时不报错)。
这些方法极大简化了常见的文件操作,推荐在Java 7+环境中优先使用。
六、IO流的异常处理与资源管理
IO操作可能抛出IOException
(受检异常),必须妥善处理;同时,流对象占用系统资源,必须确保关闭。
1. 传统异常处理:try-catch-finally
在Java 7之前,需在finally
块中关闭流,确保资源释放:
FileInputStream fis = null;
try {
fis = new FileInputStream("file.txt");
// 读取操作
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) { // 避免空指针异常
try {
fis.close(); // 关闭流可能也会抛异常
} catch (IOException e) {
e.printStackTrace();
}
}
}
这种方式代码冗长,容易出错。
2. 自动资源管理:try-with-resources(Java 7+)
Java 7引入的try-with-resources语句可自动关闭实现AutoCloseable
接口的资源(所有IO流都实现了该接口),简化代码:
// 资源在try后的括号中声明,会自动关闭
try (FileInputStream fis = new FileInputStream("file.txt");
FileOutputStream fos = new FileOutputStream("copy.txt")) {
// 读写操作
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
// 无需手动关闭,资源会在try块结束后自动关闭
优势:
- 代码更简洁,减少模板代码。
- 确保资源关闭,即使发生异常。
- 支持多个资源声明,用分号分隔。
3. 异常处理最佳实践
-
捕获具体异常:尽量捕获
FileNotFoundException
、EOFException
等具体异常,而非笼统的IOException
,便于定位问题。 -
记录异常信息:使用日志框架(如SLF4J)记录异常堆栈,而非仅打印到控制台。
-
资源释放优先:无论操作成功与否,必须确保资源释放(try-with-resources是最佳选择)。
-
大文件处理:处理大文件时,避免一次性读取全部内容到内存(如
Files.readAllLines()
),应使用缓冲流分块读写。
七、IO性能优化与最佳实践
在处理大量数据或高并发场景时,IO性能至关重要。以下是提升IO效率的关键技巧:
1. 选择合适的流类型
- 二进制文件:优先使用字节流(如
FileInputStream
+BufferedInputStream
)。 - 文本文件:使用字符流+指定编码(如
BufferedReader
+InputStreamReader
)。 - 大文件:使用NIO的
FileChannel
和transferTo()
方法(支持零拷贝)。 - 基本数据类型:使用数据流(
DataInputStream
/DataOutputStream
)。 - 对象持久化:使用对象流(
ObjectInputStream
/ObjectOutputStream
)。
2. 使用缓冲流提升效率
缓冲流(BufferedInputStream
、BufferedReader
等)通过减少磁盘IO次数显著提升性能,务必使用。
- 缓冲区大小:默认8KB,可根据文件类型调整(如大文件用16KB或32KB)。
- 手动刷新:缓冲输出流在关闭前需调用
flush()
,确保数据写入磁盘(try-with-resources会自动刷新)。
3. 减少IO操作次数
- 批量读写:使用数组/缓冲区批量读写,避免逐个字节/字符操作。
- 合并文件操作:多次小文件写入合并为一次大文件写入。
- 避免频繁创建流:流的创建和关闭有开销,重复操作同一文件时应复用流。
4. 处理大文件的策略
- 分块读取:使用固定大小的缓冲区分块读取,避免一次性加载整个文件到内存。
- NIO零拷贝:大文件复制优先使用
FileChannel.transferTo()
,利用操作系统的零拷贝机制,减少用户态与内核态的数据拷贝。 - 异步IO:使用NIO的非阻塞IO或Java 7的
AsynchronousFileChannel
,在处理多个大文件时避免阻塞。
示例15:使用AsynchronousFileChannel异步读取大文件
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class AsyncFileChannelDemo {
public static void main(String[] args) {
Path path = Paths.get("large_file.txt");
try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
path, StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(4096);
long position = 0;
// 方式1:使用Future获取结果
Future<Integer> result = channel.read(buffer, position);
while (!result.isDone()) {
// 等待期间可做其他事情
System.out.println("等待读取完成...");
}
int bytesRead = result.get();
System.out.println("方式1:读取字节数:" + bytesRead);
// 方式2:使用CompletionHandler回调
buffer.clear();
channel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer bytesRead, ByteBuffer attachment) {
System.out.println("方式2:读取字节数:" + bytesRead);
attachment.flip();
// 处理数据...
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
exc.printStackTrace();
}
});
// 等待异步操作完成(实际开发中需更合理的同步机制)
Thread.sleep(1000);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
5. 编码与解码的最佳实践
- 显式指定编码:始终在字符流转换时指定编码(如
UTF-8
),避免依赖平台默认编码。 - 统一编码标准:项目中统一使用一种编码(如UTF-8),避免混合编码导致乱码。
- 处理BOM:UTF-8 BOM(字节顺序标记)可能导致问题,读取时可跳过BOM字节。
6. 避免常见错误
- 资源未关闭:务必使用try-with-resources确保流关闭,避免文件句柄泄漏。
- 乱码问题:字符流未指定编码,或读写编码不一致。
- 缓冲区未翻转:NIO中
ByteBuffer
写入后未调用flip()
就读取,导致数据错误。 - 序列化版本号缺失:未显式声明
serialVersionUID
,导致类结构变化后反序列化失败。
八、总结:掌握IO流,打通数据交互通道
Java IO流是程序与外部世界交互的基础,从简单的文件读写到复杂的网络通信,都离不开对数据流的处理。本文系统介绍了Java IO的核心概念、体系结构及实战用法,涵盖了字节流、字符流、数据流、对象流和NIO等关键技术。
核心要点回顾:
- 流的分类:按数据单位分为字节流和字符流,按角色分为节点流和处理流。
- 字节流:适用于所有文件类型,
FileInputStream
/FileOutputStream
是基础,缓冲流可提升效率。 - 字符流:专为文本设计,需注意编码,
BufferedReader
/BufferedWriter
提供按行读写功能。 - 数据流与对象流:分别用于处理基本数据类型和对象的序列化/反序列化。
- NIO:基于通道和缓冲区,支持非阻塞IO和高效文件操作,NIO.2的
Files
和Path
简化了文件处理。 - 资源管理:使用try-with-resources自动关闭资源,避免泄漏。
- 性能优化:使用缓冲流、批量操作、NIO零拷贝等技术提升IO效率。
掌握IO流的使用不仅能解决日常开发中的文件处理问题,也是理解分布式系统、网络编程等高级主题的基础。在实际开发中,应根据具体场景选择合适的IO方式,遵循最佳实践,编写高效、可靠的IO代码。