View RSS Feed

Apache Commons

Apache IOUtils

Rate this Entry
by , 02-24-2012 at 07:57 PM (1226 Views)
These utility methods are provided by the IOUtils:

1. Reading
2. Copying
3. Writing

Given methods work at:

1. InputStream
2. OutputStream
3. Writer
4. Reader

Bytes would be read a URL by the help of this code and will make them printed.
Java Code: This is the code to explain IOUtils
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();
 }

//IOUtils classes could be used to rewrite this code as following:

InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
 try {
   System.out.println( IOUtils.toString( in ) );
 } finally {
   IOUtils.closeQuietly(in);
 }

Submit "Apache IOUtils" to Facebook Submit "Apache IOUtils" to Digg Submit "Apache IOUtils" to del.icio.us Submit "Apache IOUtils" to StumbleUpon Submit "Apache IOUtils" to Google

Categories
Commons IO

Comments