一、基础文件读写操作
1.1 字节流读取文本文件(通用文件处理)
字节流是Java IO体系中最基础的组件,其InputStream和OutputStream接口可处理任意类型文件。读取文本文件时需注意编码转换,推荐使用InputStreamReader进行字符解码:
try (FileInputStream fis = new FileInputStream("test.txt");InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);BufferedReader br = new BufferedReader(isr)) {String line;while ((line = br.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}
关键点:
- 三层包装结构:
FileInputStream→InputStreamReader→BufferedReader - 字符编码通过
StandardCharsets指定,避免平台依赖 - 使用try-with-resources确保资源自动释放
1.2 字符流写入日志文件(高效文本输出)
对于纯文本输出场景,字符流提供更直观的API。通过PrintWriter可简化格式化操作:
try (PrintWriter writer = new PrintWriter(new FileWriter("app.log", StandardCharsets.UTF_8, true))) {writer.printf("[%s] INFO: User %s logged in%n",LocalDateTime.now(), "admin");} catch (IOException e) {System.err.println("Log write failed: " + e.getMessage());}
优化建议:
- 追加模式通过
FileWriter构造函数参数控制 - 使用
printf替代字符串拼接提升可读性 - 生产环境建议集成日志框架(如SLF4J)
二、二进制数据处理
2.1 字节流复制多媒体文件(高效大文件传输)
处理图片、视频等二进制文件时,字节流是唯一选择。使用缓冲流可显著提升性能:
public static void copyFile(String src, String dest) throws IOException {try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest))) {byte[] buffer = new byte[8192]; // 8KB缓冲区int bytesRead;while ((bytesRead = bis.read(buffer)) != -1) {bos.write(buffer, 0, bytesRead);}}}
性能考量:
- 缓冲区大小建议为8KB的整数倍
- 避免频繁的小数据量读写
- 使用NIO的
Files.copy()可获得更好性能(Java 7+)
2.2 数据流序列化对象(持久化存储)
DataOutputStream和DataInputStream提供基本数据类型的读写方法:
// 写入对象try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {dos.writeInt(100);dos.writeDouble(3.14);dos.writeUTF("Hello World");}// 读取对象(需保持写入顺序)try (DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {int num = dis.readInt();double pi = dis.readDouble();String str = dis.readUTF();}
注意事项:
- 读写顺序必须严格一致
- 不支持复杂对象图序列化
- 生产环境建议使用JSON/XML或专用序列化框架
三、高级IO技术
3.1 内存映射文件(超大文件处理)
对于GB级文件,内存映射可避免JVM堆内存溢出:
try (RandomAccessFile file = new RandomAccessFile("large.dat", "rw");FileChannel channel = file.getChannel()) {MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024); // 映射1MBbuffer.put((byte) 0x41); // 写入数据} catch (IOException e) {e.printStackTrace();}
适用场景:
- 需要随机访问的超大文件
- 高性能要求的数据处理
- 数据库文件等I/O密集型应用
3.2 压缩流处理(节省存储空间)
通过GZIPOutputStream实现实时压缩:
// 压缩文件try (FileOutputStream fos = new FileOutputStream("compressed.gz");GZIPOutputStream gzip = new GZIPOutputStream(fos);BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(gzip, StandardCharsets.UTF_8))) {writer.write("This will be compressed...");}// 解压文件try (FileInputStream fis = new FileInputStream("compressed.gz");GZIPInputStream gzip = new GZIPInputStream(fis);BufferedReader reader = new BufferedReader(new InputStreamReader(gzip, StandardCharsets.UTF_8))) {String line;while ((line = reader.readLine()) != null) {System.out.println(line);}}
压缩效率:
- 文本文件通常可压缩60-80%
- 二进制文件压缩率取决于数据特征
- 多线程环境需同步压缩流操作
四、异常处理最佳实践
4.1 资源泄漏防护
始终使用try-with-resources或finally块确保资源释放:
// 传统方式(Java 6及之前)FileInputStream fis = null;try {fis = new FileInputStream("file.txt");// 处理逻辑} catch (IOException e) {// 异常处理} finally {if (fis != null) {try {fis.close();} catch (IOException e) {// 记录关闭异常}}}
4.2 自定义异常处理
对于复杂业务逻辑,建议封装专用异常:
public class FileProcessingException extends IOException {public FileProcessingException(String message, Throwable cause) {super(message, cause);}// 添加业务相关字段和方法}// 使用示例try {processFile();} catch (FileProcessingException e) {logger.error("File processing failed: {}", e.getMessage(), e);// 业务降级处理}
五、性能优化策略
5.1 缓冲流选择指南
| 流类型 | 适用场景 | 缓冲区大小推荐 |
|---|---|---|
| BufferedInputStream | 通用字节流处理 | 8KB-64KB |
| BufferedReader | 文本行读取 | 8KB |
| BufferedOutputStream | 通用字节流输出 | 8KB-64KB |
5.2 NIO替代方案
对于高并发场景,考虑使用Java NIO:
// NIO文件复制示例Path source = Paths.get("source.txt");Path target = Paths.get("target.txt");try {Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);} catch (IOException e) {e.printStackTrace();}
NIO优势:
- 非阻塞I/O模型
- 通道(Channel)和缓冲区(Buffer)机制
- 文件锁等高级功能支持
六、典型应用场景
- 日志系统:字符流+缓冲+滚动策略
- 配置管理:属性文件读写
- 数据交换:CSV/JSON文件处理
- 多媒体处理:图片/视频文件操作
- 网络传输:Socket数据流处理
本文通过13个代码示例系统展示了Java IO流的核心技术,开发者可根据实际需求选择合适的流类型和优化策略。对于企业级应用,建议结合日志框架、监控告警等配套设施构建健壮的文件处理系统。