Skip to content

Apache Commons IO

Developer UtilitiesUtilities/IOJava

What it is

Apache Commons IO provides utilities for working with Java I/O, including file and directory manipulation, file filters, input/output stream handling, and file monitoring. It simplifies common I/O operations and reduces boilerplate code.

Commons IO provides utilities for file operations (copy, move, delete), directory traversal, filters, input/output streams, and monitoring. It helps avoid manual boilerplate code for common I/O tasks.

Installation

Add dependency in pom.xml: <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.15.0</version> </dependency>

Getting started

The smallest useful thing you can do with it, and what each part means.

Copying a file
import org.apache.commons.io.FileUtils;
import java.io.File;

File source = new File("source.txt");
File dest = new File("dest.txt");
FileUtils.copyFile(source, dest);
Copies a file from source to destination.
Reading file content
String content = FileUtils.readFileToString(new File("example.txt"), "UTF-8");
System.out.println(content);
Reads the entire content of a file as a String.

Advanced usage

Where the library earns its place over a simpler alternative.

Directory traversal
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Collection;

Collection<File> files = FileUtils.listFiles(new File("/path/to/dir"), new String[]{"txt", "java"}, true);
for (File file : files) {
    System.out.println(file.getName());
}
Lists all files with specified extensions in a directory recursively.
File monitoring
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

FileAlterationObserver observer = new FileAlterationObserver("/path/to/dir");
observer.addListener(new MyFileListener());
FileAlterationMonitor monitor = new FileAlterationMonitor(5000, observer);
monitor.start();
Monitors a directory for changes using FileAlterationMonitor.
Stream utilities
import org.apache.commons.io.IOUtils;
import java.io.FileInputStream;

try (FileInputStream fis = new FileInputStream("input.txt")) {
    String content = IOUtils.toString(fis, "UTF-8");
    System.out.println(content);
}
Reads the content of an InputStream into a String safely using IOUtils.
Deleting directories
FileUtils.deleteDirectory(new File("/path/to/delete"));
Recursively deletes a directory and its contents.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

IOException
Occurs when file or stream operations fail. Ensure paths exist, have correct permissions, and streams are properly handled.
IllegalArgumentException
Thrown when invalid arguments like null files are passed. Validate inputs before calling utility methods.

Best practices

  • Use FileUtils and IOUtils to reduce boilerplate and improve readability.
  • Always close streams or use try-with-resources to avoid resource leaks.
  • Use FileAlterationMonitor for efficient directory monitoring instead of polling manually.
  • Validate paths before performing delete or write operations to prevent accidental data loss.
  • Combine Commons IO with NIO2 APIs for performance-critical applications.

Background

Why it exists, and what it was reacting to.

Commons IO was created to provide a robust set of tools for file, stream, and directory operations in Java. It includes classes to copy files, read/write content, monitor directories, apply filters, and work with streams efficiently. It is widely used in Java applications where I/O operations are frequent and complex, making code cleaner and safer.