Class IOUtils

java.lang.Object
org.apache.commons.io.IOUtils

public class IOUtils extends Object
General IO stream manipulation utilities.

This class provides static utility methods for input/output operations.

  • closeQuietly - these methods close a stream ignoring nulls and exceptions
  • toXxx/read - these methods read data from a stream
  • write - these methods write data to a stream
  • copy - these methods copy all the data from one stream to another
  • contentEquals - these methods compare the content of two streams

The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.

All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader. The default buffer size of 4K has been shown to be efficient in tests.

The various copy methods all delegate the actual copying to one of the following methods:

For example, copy(InputStream, OutputStream) calls copyLarge(InputStream, OutputStream) which calls copy(InputStream, OutputStream, int) which creates the buffer and calls copyLarge(InputStream, OutputStream, byte[]).

Applications can re-use buffers by using the underlying methods directly. This may improve performance for applications that need to do a lot of copying.

Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.

Provenance: Excalibur.