If you've ever had to copy a File
or copy the contents of an InputStream
to an OutputStream
, you've probably
wondered why Java goes out of its way to make things difficult. Java I/O
is not a terribly complex subject, but it does have a knack for turning
simpler tasks into complex nests of heavily wrapped Reader
s and streams. Commons IO fills a few gaps
in Java's I/O and networking capabilities by providing utilities and
methods to copy streams, copy files, touch files, recursively delete
directories, and safely close Reader
s
and streams. If you are working with Reader
, Writer
, InputStream
, or OutputStream
, you should take a look at IOUtils
and CopyUtils
; they may save you a few lines of
tedious code.
Commons IO also provides a set of simple FilenameFilter
implementations, which can be
used to selectively list files or directories. In addition to Commons IO's
FilenameFilter
implementations, Jakarta
ORO's GlobFilenameFilter
and Perl5FilenameFilter
are presented to introduce
you to more complex filters involving expressions. ORO is a subproject of
Jakarta that provides support for Perl 5 regular expressions and glob
expressions. A glob expression is commonly used when listing files in a
directory; for example, the expression, *.xml
, is a glob expression that matches every
file that ends in .xml
. While Java
1.4 provides support for regular expressions, there are subtle differences
between the regular expression syntax supported by Java 1.4 and the
regular expression syntax supported in Perl 5. You can learn more about
the differences between Perl5 regular expressions supported by ORO and
regular expressions supported by Java 1.4 by reading the ORO project page
at http://jakarta.apache.org/oro.
Take a look at Jakarta ORO if your application needs to work with globs
and complex regular expressions.
Commons Net contains simple clients for common protocols, such as
FTP, POP3, and SMTP. Using Commons Net, you can retrieve or transfer files
to an FTP server with a very small amount of code. Sun provides a very
capable set of classes to send and retrieve mail using POP and SMTP, but
the javax.mail
API brings a certain
amount of complexity and overhead that might not make sense for an
application sending a simple email message. Commons Net provides a
lightweight SMTP client, which can be used to send a simple email message
in a few lines of code without introducing the complexity of javax.mail
. Commons Net also contains a very
straightforward POP3 client, which can be used to check a POP mailbox for
incoming messages. In addition to FTP, POP, and SMTP, Commons Net contains
simple clients for Trivial File Transfer Protocol (TFTP), Telnet, Finger,
and NNTP.