Summary
The Apache Commons IO project is a Java library that simplifies working with all things related to streams, files, and even the local file system. In some cases, Commons IO brings Ruby-like simplicity to working with Java IO objects.
Advertisement
While some developers looking at dynamic languages bemoan Java's comparative verbosity, others seem busily at work bringing similar conciseness to Java code.
One example is the Apache Commons IO project, which recently released the 1.3.1 version of its Java library. Commons IO makes working with streams and files easier, often requiring just a fraction of code compared with the standard Java IO APIs. The project's documentation shows how dramatically Commons IO can reduce the amount of code required for reading from an input stream, for example:
Without commons IO:
InputStream in = new URL( "http://jakarta.apache.org").openStream();
try {
InputStreamReader inR = new InputStreamReader( in );
BufferedReader buf = new BufferedReader( inR );
String line;
while ( ( line = buf.readLine() ) != null ) {
System.out.println( line );
}
} finally {
in.close();
}
With Commons IO:
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}
Reading lines of a file becomes similarly easy:
File file = new File("/commons/io/project.properties");
List lines = FileUtils.readLines(file, "UTF-8");
Such code is clearly on par with the sort of simplicity that dynamic languages, such as Ruby, are known for. Do you believe that APIs, such as Commons IO, can solve Java's verbosity problems in a way the language can't?
/* Do you believe that APIs, such as Commons IO, can solve Java's verbosity problems in a way the language can't? */
Only if you accept the idea that some programmers are meant to create libraries (and suffer from the verbosity) and other programmers are meant to write applications (and benefit from the library programmer who took one for the team). Otherwise, Java's going to need some better abstraction mechanisms.