Top Description Fields Constructors Methods
org.apache.commons.io

public Class IOUtils

extends Object
Class Inheritance
Imports
java.io.BufferedInputStream, .BufferedReader, .ByteArrayInputStream, .CharArrayWriter, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .PrintWriter, .Reader, .StringWriter, .Writer, java.util.ArrayList, .Collection, .Iterator, .List, org.apache.commons.io.output.ByteArrayOutputStream

General IO stream manipulation utilities.

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

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.

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.

Origin of code: Excalibur.

Authors
Peter Donald, Jeff Turner, Matthew Hawthorne, Stephen Colebourne, Gareth Davis, Ian Springer, Niall Pemberton, Sandy McArthur
Version
$Id: IOUtils.java 481854 2006-12-03 18:30:07Z scolebourne $

Field Summary

Modifier and TypeField and Description
private static final int
DEFAULT_BUFFER_SIZE

The default buffer size to use.

public static final char
DIR_SEPARATOR

The system directory separator character.

public static final char
DIR_SEPARATOR_UNIX

The Unix directory separator character.

public static final char
DIR_SEPARATOR_WINDOWS

The Windows directory separator character.

public static final String
LINE_SEPARATOR

The system line separator string.

public static final String
LINE_SEPARATOR_UNIX

The Unix line separator string.

public static final String
LINE_SEPARATOR_WINDOWS

The Windows line separator string.

Constructor Summary

AccessConstructor and Description
public
IOUtils()

Instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static void
closeQuietly(Reader
the Reader to close, may be null or already closed
input
)

Unconditionally close an Reader.

public static void
closeQuietly(Writer
the Writer to close, may be null or already closed
output
)

Unconditionally close a Writer.

public static void
closeQuietly(InputStream
the InputStream to close, may be null or already closed
input
)

Unconditionally close an InputStream.

public static void
closeQuietly(OutputStream
the OutputStream to close, may be null or already closed
output
)

Unconditionally close an OutputStream.

public static boolean

Returns:

true if the content of the streams are equal or they both don't exist, false otherwise
contentEquals
(InputStream
the first stream
input1
,
InputStream
the second stream
input2
)

Compare the contents of two Streams to determine if they are equal or not.

public static boolean

Returns:

true if the content of the readers are equal or they both don't exist, false otherwise
contentEquals
(Reader
the first reader
input1
,
Reader
the second reader
input2
)

Compare the contents of two Readers to determine if they are equal or not.

public static int

Returns:

the number of bytes copied
copy
(InputStream
the InputStream to read from
input
,
OutputStream
the OutputStream to write to
output
)

Copy bytes from an InputStream to an OutputStream.

public static void
copy(InputStream
the InputStream to read from
input
,
Writer
the Writer to write to
output
)

Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.

public static void
copy(InputStream
the InputStream to read from
input
,
Writer
the Writer to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Copy bytes from an InputStream to chars on a Writer using the specified character encoding.

public static int

Returns:

the number of characters copied
copy
(Reader
the Reader to read from
input
,
Writer
the Writer to write to
output
)

Copy chars from a Reader to a Writer.

public static void
copy(Reader
the Reader to read from
input
,
OutputStream
the OutputStream to write to
output
)

Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

public static void
copy(Reader
the Reader to read from
input
,
OutputStream
the OutputStream to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

public static long

Returns:

the number of bytes copied
copyLarge
(InputStream
the InputStream to read from
input
,
OutputStream
the OutputStream to write to
output
)

Copy bytes from a large (over 2GB) InputStream to an OutputStream.

public static long

Returns:

the number of characters copied
copyLarge
(Reader
the Reader to read from
input
,
Writer
the Writer to write to
output
)

Copy chars from a large (over 2GB) Reader to a Writer.

public static LineIterator

Returns:

an Iterator of the lines in the reader, never null
lineIterator
(Reader
the Reader to read from, not null
reader
)

Return an Iterator for the lines in a Reader.

public static LineIterator

Returns:

an Iterator of the lines in the reader, never null
lineIterator
(InputStream
the InputStream to read from, not null
input
,
String
the encoding to use, null means platform default
encoding
)

Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).

public static List<E>

Returns:

the list of Strings, never null
readLines
(InputStream
the InputStream to read from, not null
input
)

Get the contents of an InputStream as a list of Strings, one entry per line, using the default character encoding of the platform.

public static List<E>

Returns:

the list of Strings, never null
readLines
(InputStream
the InputStream to read from, not null
input
,
String
the encoding to use, null means platform default
encoding
)

Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.

public static List<E>

Returns:

the list of Strings, never null
readLines
(Reader
the Reader to read from, not null
input
)

Get the contents of a Reader as a list of Strings, one entry per line.

public static byte[]

Returns:

the requested byte array
toByteArray
(InputStream
the InputStream to read from
input
)

Get the contents of an InputStream as a byte[].

public static byte[]

Returns:

the requested byte array
toByteArray
(Reader
the Reader to read from
input
)

Get the contents of a Reader as a byte[] using the default character encoding of the platform.

public static byte[]

Returns:

the requested byte array
toByteArray
(Reader
the Reader to read from
input
,
String
the encoding to use, null means platform default
encoding
)

Get the contents of a Reader as a byte[] using the specified character encoding.

public static byte[]

Returns:

the requested byte array
toByteArray
(String
the String to convert
input
)

Deprecated Use String#getBytes()
Get the contents of a String as a byte[] using the default character encoding of the platform.
public static char[]

Returns:

the requested character array
toCharArray
(InputStream
the InputStream to read from
is
)

Get the contents of an InputStream as a character array using the default character encoding of the platform.

public static char[]

Returns:

the requested character array
toCharArray
(InputStream
the InputStream to read from
is
,
String
the encoding to use, null means platform default
encoding
)

Get the contents of an InputStream as a character array using the specified character encoding.

public static char[]

Returns:

the requested character array
toCharArray
(Reader
the Reader to read from
input
)

Get the contents of a Reader as a character array.

public static InputStream

Returns:

an input stream
toInputStream
(String
the string to convert
input
)

Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.

public static InputStream

Returns:

an input stream
toInputStream
(String
the string to convert
input
,
String
the encoding to use, null means platform default
encoding
)

Convert the specified string to an input stream, encoded as bytes using the specified character encoding.

public static String

Returns:

the requested String
toString
(InputStream
the InputStream to read from
input
)

Get the contents of an InputStream as a String using the default character encoding of the platform.

public static String

Returns:

the requested String
toString
(InputStream
the InputStream to read from
input
,
String
the encoding to use, null means platform default
encoding
)

Get the contents of an InputStream as a String using the specified character encoding.

public static String

Returns:

the requested String
toString
(Reader
the Reader to read from
input
)

Get the contents of a Reader as a String.

public static String

Returns:

the requested String
toString
(byte[]
the byte array to read from
input
)

Deprecated Use String#String(byte[])
Get the contents of a byte[] as a String using the default character encoding of the platform.
public static String

Returns:

the requested String
toString
(byte[]
the byte array to read from
input
,
String
the encoding to use, null means platform default
encoding
)

Get the contents of a byte[] as a String using the specified character encoding.
public static void
write(byte[]
the byte array to write, do not modify during output, null ignored
data
,
OutputStream
the OutputStream to write to
output
)

Writes bytes from a byte[] to an OutputStream.

public static void
write(byte[]
the byte array to write, do not modify during output, null ignored
data
,
Writer
the Writer to write to
output
)

Writes bytes from a byte[] to chars on a Writer using the default character encoding of the platform.

public static void
write(byte[]
the byte array to write, do not modify during output, null ignored
data
,
Writer
the Writer to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Writes bytes from a byte[] to chars on a Writer using the specified character encoding.

public static void
write(char[]
the char array to write, do not modify during output, null ignored
data
,
Writer
the Writer to write to
output
)

Writes chars from a char[] to a Writer using the default character encoding of the platform.

public static void
write(char[]
the char array to write, do not modify during output, null ignored
data
,
OutputStream
the OutputStream to write to
output
)

Writes chars from a char[] to bytes on an OutputStream.

public static void
write(char[]
the char array to write, do not modify during output, null ignored
data
,
OutputStream
the OutputStream to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Writes chars from a char[] to bytes on an OutputStream using the specified character encoding.

public static void
write(String
the String to write, null ignored
data
,
Writer
the Writer to write to
output
)

Writes chars from a String to a Writer.

public static void
write(String
the String to write, null ignored
data
,
OutputStream
the OutputStream to write to
output
)

Writes chars from a String to bytes on an OutputStream using the default character encoding of the platform.

public static void
write(String
the String to write, null ignored
data
,
OutputStream
the OutputStream to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Writes chars from a String to bytes on an OutputStream using the specified character encoding.

public static void
write(StringBuffer
the StringBuffer to write, null ignored
data
,
Writer
the Writer to write to
output
)

Writes chars from a StringBuffer to a Writer.

public static void
write(StringBuffer
the StringBuffer to write, null ignored
data
,
OutputStream
the OutputStream to write to
output
)

Writes chars from a StringBuffer to bytes on an OutputStream using the default character encoding of the platform.

public static void
write(StringBuffer
the StringBuffer to write, null ignored
data
,
OutputStream
the OutputStream to write to
output
,
String
the encoding to use, null means platform default
encoding
)

Writes chars from a StringBuffer to bytes on an OutputStream using the specified character encoding.

public static void
writeLines(Collection<E>
the lines to write, null entries produce blank lines
lines
,
String
the line separator to use, null is system default
lineEnding
,
OutputStream
the OutputStream to write to, not null, not closed
output
)

Writes the toString() value of each item in a collection to an OutputStream line by line, using the default character encoding of the platform and the specified line ending.

public static void
writeLines(Collection<E>
the lines to write, null entries produce blank lines
lines
,
String
the line separator to use, null is system default
lineEnding
,
OutputStream
the OutputStream to write to, not null, not closed
output
,
String
the encoding to use, null means platform default
encoding
)

Writes the toString() value of each item in a collection to an OutputStream line by line, using the specified character encoding and the specified line ending.

public static void
writeLines(Collection<E>
the lines to write, null entries produce blank lines
lines
,
String
the line separator to use, null is system default
lineEnding
,
Writer
the Writer to write to, not null, not closed
writer
)

Writes the toString() value of each item in a collection to a Writer line by line, using the specified line ending.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait