全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-08 6 分钟 ✍️ juanwangdev

IOUtils

IOUtils是Apache Commons IO提供的IO流工具类,简化流操作。

概述

所在包

org.apache.commons.io.IOUtils

依赖引入

XML
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

核心特点

  • 静态方法,无需创建对象
  • 空安全,null流不会抛异常
  • 简化IO代码,一行完成复杂操作

读取流

toString读取字符串

Java
// 读取流为字符串
InputStream is = new FileInputStream("test.txt");
String content = IOUtils.toString(is, "UTF-8");
is.close();

// 读取URL内容
String html = IOUtils.toString(new URL("https://example.com"), "UTF-8");

readLines按行读取

Java
InputStream is = new FileInputStream("test.txt");
List<String> lines = IOUtils.readLines(is, "UTF-8");
is.close();

// 每行一个String元素
for (String line : lines) {
    System.out.println(line);
}

toByteArray读取字节

Java
InputStream is = new FileInputStream("image.jpg");
byte[] data = IOUtils.toByteArray(is);
is.close();

// 返回字节数组,适合二进制数据

写入流

write写入

Java
OutputStream os = new FileOutputStream("test.txt");
IOUtils.write("Hello World", os, "UTF-8");
os.close();

// 写入字节数组
OutputStream os = new FileOutputStream("data.bin");
IOUtils.write(bytes, os);
os.close();

writeLines按行写入

Java
OutputStream os = new FileOutputStream("test.txt");
List<String> lines = Arrays.asList("line1", "line2", "line3");
IOUtils.writeLines(lines, null, os, "UTF-8");  // null表示不额外添加换行符
os.close();

复制流

copy复制流

Java
InputStream is = new FileInputStream("source.txt");
OutputStream os = new FileOutputStream("target.txt");

// 复制流
long bytesCopied = IOUtils.copy(is, os);
// 返回复制的字节数

is.close();
os.close();

copyLarge大文件复制

Java
InputStream is = new FileInputStream("large.mp4");
OutputStream os = new FileOutputStream("copy.mp4");

// 复制大文件(超过2GB)
long bytesCopied = IOUtils.copyLarge(is, os);

is.close();
os.close();

copyLarge返回long类型,支持大于2GB文件,copy返回int类型有上限。

关闭流

closeQuietly安全关闭

Java
InputStream is = null;
OutputStream os = null;
try {
    is = new FileInputStream("source.txt");
    os = new FileOutputStream("target.txt");
    IOUtils.copy(is, os);
} finally {
    // 安静关闭,忽略异常
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(os);
}

注意:closeQuietly已废弃,推荐使用try-with-resources。

try-with-resources替代

Java
// Java 7+推荐方式
try (
    InputStream is = new FileInputStream("source.txt");
    OutputStream os = new FileOutputStream("target.txt")
) {
    IOUtils.copy(is, os);
}  // 自动关闭

内容比较

contentEquals比较内容

Java
InputStream is1 = new FileInputStream("file1.txt");
InputStream is2 = new FileInputStream("file2.txt");

boolean same = IOUtils.contentEquals(is1, is2);
// true/false(比较流内容是否相同)

is1.close();
is2.close();

其他方法

skip跳过字节

Java
InputStream is = new FileInputStream("test.txt");
long skipped = IOUtils.skip(is, 100);  // 跳过100字节
// 返回实际跳过的字节数
is.close();

read读取到缓冲区

Java
InputStream is = new FileInputStream("test.txt");
byte[] buffer = new byte[1024];
int read = IOUtils.read(is, buffer);
// 返回实际读取的字节数
is.close();

要点总结

  • IOUtils是Apache Commons IO的流操作工具类
  • toString读取流为字符串
  • readLines按行读取返回List
  • toByteArray读取为字节数组
  • write/writeLines写入内容
  • copy/copyLarge复制流
  • closeQuietly安全关闭(已废弃,推荐try-with-resources)
  • contentEquals比较流内容

📝 发现内容有误?点击此处直接编辑

← 上一篇 FileUtils
下一篇 → MathUtils
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库