Apache Commons IO - Overview


Advertisements

Apache Commons IO library provides various utility classes for common operations for File IO covering wide range of use cases. It helps avoid writing boilerplate code.

Classes

Apache Commons IO library provides classes for following categories −

Utility classes

These classes which are under org.apache.commons.io package, provides file and string comparison. Following are some of the examples.

  • IOUtils − Provides utility methods for reading, writing and copying files. The methods work with InputStream, OutputStream, Reader and Writer.

  • FilenameUtils − Provides method to work with file names without using File Object. It works on different operating systems in similar way.

  • FileUtils − Provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object.

  • IOCase − Provides method for string manipulation and comparison.

  • FileSystemUtils − Provides method to get the free space on a disk drive.

  • LineIterator − Provides a flexible way to work with a line-based file.

Filter classes

Filter classes which are under org.apache.commons.io.filefilter package, provides methods to filter files based on logical criteria instead of string based tedious comparisons. Following are some of the examples.

  • NameFileFilter − Filters file-names for a name.

  • WildcardFileFilter − Filters files using the supplied wildcards.

  • SuffixFileFilter − Filters files based on suffix. This is used in retrieving all the files of a particular type.

  • PrefixFileFilter − Filters files based on prefix.

  • OrFileFilter − Provides conditional OR logic across a list of file filters. Returns true, if any filters in the list return true. Otherwise, it returns false.

  • AndFileFilter − Provides conditional and logic across a list of file filters. Returns false if any filters in the list return false. Otherwise, it returns true.

File Monitor classes

File monitor classes which are under org.apache.commons.io.monitor package, provides control to track changes in a specific file or folder and allows to do action accordingly on the changes. Following are some of the examples.

  • FileEntry − Provides the state of a file or directory. File attributes at a point in time.

  • FileAlterationObserver − Represents the state of files below a root directory, checks the file system and notifies listeners of create, change or delete events.

  • FileAlterationMonitor − Represents a thread that spawns a monitoring thread triggering any registered FileAlterationObserver at a specified interval.

Comparator classes

File monitor classes under org.apache.commons.io.comparator package allow to compare and sort files and directories easily.

  • NameFileComparator − Compare the names of two files.

  • SizeFileComparator − Compare the size of two files.

  • LastModifiedFileComparator − Compare the last modified dates of two files.

Stream classes

There are multiple implementation of InputStream under org.apache.commons.io.input package and of OutputStream under org.apache.commons.io.output package, to do useful tasks on streams. Following are some of the examples.

  • NullOutputStream − Absorbs all data sent with any error.

  • TeeOutputStream − Sends output to two streams.

  • ByteArrayOutputStream − Faster version of JDK class.

  • CountingOutputStream − Counts the number of bytes passed through the stream.

  • ProxyOutputStream − Changes the calls to proxy stream.

  • LockableFileWriter − A FileWriter to create lock files and allow simple cross thread file lock handling.

Advertisements