Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.commons.io.IOUtils
public class IOUtils
extends java.lang.Object
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.
Field Summary | |
private static int |
|
static char |
|
static char |
|
static char |
|
static String |
|
static String |
|
static String |
|
Constructor Summary | |
|
Method Summary | |
static void |
|
static void |
|
static void |
|
static void |
|
static boolean |
|
static boolean |
|
static int |
|
static void |
|
static void |
|
static void |
|
static void |
|
static int |
|
static long |
|
static long |
|
static LineIterator |
|
static LineIterator |
|
static List |
|
static List |
|
static List |
|
static byte[] |
|
static byte[] |
|
static byte[] |
|
static byte[] |
|
static char[] |
|
static char[] |
|
static char[] |
|
static InputStream |
|
static InputStream |
|
static String |
|
static String |
|
static String |
|
static String |
|
static String |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
static void |
|
private static final int DEFAULT_BUFFER_SIZE
The default buffer size to use.
- Field Value:
- 4096
public static final char DIR_SEPARATOR
The system directory separator character.
public static final char DIR_SEPARATOR_UNIX
The Unix directory separator character.
- Field Value:
- '/'
public static final char DIR_SEPARATOR_WINDOWS
The Windows directory separator character.
- Field Value:
- '\'
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.
public static void closeQuietly(InputStream input)
Unconditionally close anInputStream
. Equivalent toInputStream.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
- Parameters:
input
- the InputStream to close, may be null or already closed
public static void closeQuietly(OutputStream output)
Unconditionally close anOutputStream
. Equivalent toOutputStream.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
- Parameters:
output
- the OutputStream to close, may be null or already closed
public static void closeQuietly(Reader input)
Unconditionally close anReader
. Equivalent toReader.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
- Parameters:
input
- the Reader to close, may be null or already closed
public static void closeQuietly(Writer output)
Unconditionally close aWriter
. Equivalent toWriter.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
- Parameters:
output
- the Writer to close, may be null or already closed
public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException
Compare the contents of two Streams to determine if they are equal or not. This method buffers the input internally usingBufferedInputStream
if they are not already buffered.
- Parameters:
input1
- the first streaminput2
- the second stream
- Returns:
- true if the content of the streams are equal or they both don't exist, false otherwise
public static boolean contentEquals(Reader input1, Reader input2) throws IOException
Compare the contents of two Readers to determine if they are equal or not. This method buffers the input internally usingBufferedReader
if they are not already buffered.
- Parameters:
input1
- the first readerinput2
- the second reader
- Returns:
- true if the content of the readers are equal or they both don't exist, false otherwise
- Since:
- Commons IO 1.1
public static int copy(InputStream input, OutputStream output) throws IOException
Copy bytes from anInputStream
to anOutputStream
. This method buffers the input internally, so there is no need to use aBufferedInputStream
. Large streams (over 2GB) will return a bytes copied value of-1
after the copy has completed since the correct number of bytes cannot be returned as an int. For large streams use thecopyLarge(InputStream, OutputStream)
method.
- Parameters:
input
- theInputStream
to read fromoutput
- theOutputStream
to write to
- Returns:
- the number of bytes copied
- Since:
- Commons IO 1.1
public static void copy(InputStream input, Writer output) throws IOException
Copy bytes from anInputStream
to chars on aWriter
using the default character encoding of the platform. This method buffers the input internally, so there is no need to use aBufferedInputStream
. This method usesInputStreamReader
.
- Parameters:
input
- theInputStream
to read fromoutput
- theWriter
to write to
- Since:
- Commons IO 1.1
public static void copy(InputStream input, Writer output, String encoding) throws IOException
Copy bytes from anInputStream
to chars on aWriter
using the specified character encoding. This method buffers the input internally, so there is no need to use aBufferedInputStream
. Character encoding names can be found at IANA. This method usesInputStreamReader
.
- Parameters:
input
- theInputStream
to read fromoutput
- theWriter
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void copy(Reader input, OutputStream output) throws IOException
Copy chars from aReader
to bytes on anOutputStream
using the default character encoding of the platform, and calling flush. This method buffers the input internally, so there is no need to use aBufferedReader
. Due to the implementation of OutputStreamWriter, this method performs a flush. This method usesOutputStreamWriter
.
- Parameters:
input
- theReader
to read fromoutput
- theOutputStream
to write to
- Since:
- Commons IO 1.1
public static void copy(Reader input, OutputStream output, String encoding) throws IOException
Copy chars from aReader
to bytes on anOutputStream
using the specified character encoding, and calling flush. This method buffers the input internally, so there is no need to use aBufferedReader
. Character encoding names can be found at IANA. Due to the implementation of OutputStreamWriter, this method performs a flush. This method usesOutputStreamWriter
.
- Parameters:
input
- theReader
to read fromoutput
- theOutputStream
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static int copy(Reader input, Writer output) throws IOException
Copy chars from aReader
to aWriter
. This method buffers the input internally, so there is no need to use aBufferedReader
. Large streams (over 2GB) will return a chars copied value of-1
after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use thecopyLarge(Reader, Writer)
method.
- Parameters:
input
- theReader
to read fromoutput
- theWriter
to write to
- Returns:
- the number of characters copied
- Since:
- Commons IO 1.1
public static long copyLarge(InputStream input, OutputStream output) throws IOException
Copy bytes from a large (over 2GB)InputStream
to anOutputStream
. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read fromoutput
- theOutputStream
to write to
- Returns:
- the number of bytes copied
- Since:
- Commons IO 1.3
public static long copyLarge(Reader input, Writer output) throws IOException
Copy chars from a large (over 2GB)Reader
to aWriter
. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read fromoutput
- theWriter
to write to
- Returns:
- the number of characters copied
- Since:
- Commons IO 1.3
public static LineIterator lineIterator(InputStream input, String encoding) throws IOException
Return an Iterator for the lines in anInputStream
, using the character encoding specified (or default encoding if null).LineIterator
holds a reference to the openInputStream
specified here. When you have finished with the iterator you should close the stream to free internal resources. This can be done by closing the stream directly, or by callingLineIterator.close()
orLineIterator.closeQuietly(LineIterator)
. The recommended usage pattern is:try { LineIterator it = IOUtils.lineIterator(stream, "UTF-8"); while (it.hasNext()) { String line = it.nextLine(); /// do something with line } } finally { IOUtils.closeQuietly(stream); }
- Parameters:
input
- theInputStream
to read from, not nullencoding
- the encoding to use, null means platform default
- Returns:
- an Iterator of the lines in the reader, never null
- Since:
- Commons IO 1.2
public static LineIterator lineIterator(Reader reader)
Return an Iterator for the lines in aReader
.LineIterator
holds a reference to the openReader
specified here. When you have finished with the iterator you should close the reader to free internal resources. This can be done by closing the reader directly, or by callingLineIterator.close()
orLineIterator.closeQuietly(LineIterator)
. The recommended usage pattern is:try { LineIterator it = IOUtils.lineIterator(reader); while (it.hasNext()) { String line = it.nextLine(); /// do something with line } } finally { IOUtils.closeQuietly(reader); }
- Parameters:
reader
- theReader
to read from, not null
- Returns:
- an Iterator of the lines in the reader, never null
- Since:
- Commons IO 1.2
public static List readLines(InputStream input) throws IOException
Get the contents of anInputStream
as a list of Strings, one entry per line, using the default character encoding of the platform. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read from, not null
- Returns:
- the list of Strings, never null
- Since:
- Commons IO 1.1
public static List readLines(InputStream input, String encoding) throws IOException
Get the contents of anInputStream
as a list of Strings, one entry per line, using the specified character encoding. Character encoding names can be found at IANA. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read from, not nullencoding
- the encoding to use, null means platform default
- Returns:
- the list of Strings, never null
- Since:
- Commons IO 1.1
public static List readLines(Reader input) throws IOException
Get the contents of aReader
as a list of Strings, one entry per line. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read from, not null
- Returns:
- the list of Strings, never null
- Since:
- Commons IO 1.1
public static byte[] toByteArray(InputStream input) throws IOException
Get the contents of anInputStream
as abyte[]
. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read from
- Returns:
- the requested byte array
public static byte[] toByteArray(Reader input) throws IOException
Get the contents of aReader
as abyte[]
using the default character encoding of the platform. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read from
- Returns:
- the requested byte array
public static byte[] toByteArray(Reader input, String encoding) throws IOException
Get the contents of aReader
as abyte[]
using the specified character encoding. Character encoding names can be found at IANA. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read fromencoding
- the encoding to use, null means platform default
- Returns:
- the requested byte array
- Since:
- Commons IO 1.1
public static byte[] toByteArray(String input) throws IOException
Deprecated. Use
String.getBytes()
Get the contents of aString
as abyte[]
using the default character encoding of the platform. This is the same asString.getBytes()
.
- Parameters:
input
- theString
to convert
- Returns:
- the requested byte array
public static char[] toCharArray(InputStream is) throws IOException
Get the contents of anInputStream
as a character array using the default character encoding of the platform. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
is
- theInputStream
to read from
- Returns:
- the requested character array
- Since:
- Commons IO 1.1
public static char[] toCharArray(InputStream is, String encoding) throws IOException
Get the contents of anInputStream
as a character array using the specified character encoding. Character encoding names can be found at IANA. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
is
- theInputStream
to read fromencoding
- the encoding to use, null means platform default
- Returns:
- the requested character array
- Since:
- Commons IO 1.1
public static char[] toCharArray(Reader input) throws IOException
Get the contents of aReader
as a character array. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read from
- Returns:
- the requested character array
- Since:
- Commons IO 1.1
public static InputStream toInputStream(String input)
Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.
- Parameters:
input
- the string to convert
- Returns:
- an input stream
- Since:
- Commons IO 1.1
public static InputStream toInputStream(String input, String encoding) throws IOException
Convert the specified string to an input stream, encoded as bytes using the specified character encoding. Character encoding names can be found at IANA.
- Parameters:
input
- the string to convertencoding
- the encoding to use, null means platform default
- Returns:
- an input stream
- Since:
- Commons IO 1.1
public static String toString(InputStream input) throws IOException
Get the contents of anInputStream
as a String using the default character encoding of the platform. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read from
- Returns:
- the requested String
public static String toString(InputStream input, String encoding) throws IOException
Get the contents of anInputStream
as a String using the specified character encoding. Character encoding names can be found at IANA. This method buffers the input internally, so there is no need to use aBufferedInputStream
.
- Parameters:
input
- theInputStream
to read fromencoding
- the encoding to use, null means platform default
- Returns:
- the requested String
public static String toString(Reader input) throws IOException
Get the contents of aReader
as a String. This method buffers the input internally, so there is no need to use aBufferedReader
.
- Parameters:
input
- theReader
to read from
- Returns:
- the requested String
public static String toString(byte[] input) throws IOException
Deprecated. Use
String.String(byte[])
Get the contents of abyte[]
as a String using the default character encoding of the platform.
- Parameters:
input
- the byte array to read from
- Returns:
- the requested String
public static String toString(byte[] input, String encoding) throws IOException
Deprecated. Use
String.String(byte[],String)
Get the contents of abyte[]
as a String using the specified character encoding. Character encoding names can be found at IANA.
- Parameters:
input
- the byte array to read fromencoding
- the encoding to use, null means platform default
- Returns:
- the requested String
public static void write(String data, OutputStream output) throws IOException
Writes chars from aString
to bytes on anOutputStream
using the default character encoding of the platform. This method usesString.getBytes()
.
- Parameters:
data
- theString
to write, null ignoredoutput
- theOutputStream
to write to
- Since:
- Commons IO 1.1
public static void write(String data, OutputStream output, String encoding) throws IOException
Writes chars from aString
to bytes on anOutputStream
using the specified character encoding. Character encoding names can be found at IANA. This method usesString.getBytes(String)
.
- Parameters:
data
- theString
to write, null ignoredoutput
- theOutputStream
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(String data, Writer output) throws IOException
Writes chars from aString
to aWriter
.
- Parameters:
data
- theString
to write, null ignoredoutput
- theWriter
to write to
- Since:
- Commons IO 1.1
public static void write(StringBuffer data, OutputStream output) throws IOException
Writes chars from aStringBuffer
to bytes on anOutputStream
using the default character encoding of the platform. This method usesString.getBytes()
.
- Parameters:
data
- theStringBuffer
to write, null ignoredoutput
- theOutputStream
to write to
- Since:
- Commons IO 1.1
public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException
Writes chars from aStringBuffer
to bytes on anOutputStream
using the specified character encoding. Character encoding names can be found at IANA. This method usesString.getBytes(String)
.
- Parameters:
data
- theStringBuffer
to write, null ignoredoutput
- theOutputStream
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(StringBuffer data, Writer output) throws IOException
Writes chars from aStringBuffer
to aWriter
.
- Parameters:
data
- theStringBuffer
to write, null ignoredoutput
- theWriter
to write to
- Since:
- Commons IO 1.1
public static void write(byte[] data, OutputStream output) throws IOException
Writes bytes from abyte[]
to anOutputStream
.
- Parameters:
data
- the byte array to write, do not modify during output, null ignoredoutput
- theOutputStream
to write to
- Since:
- Commons IO 1.1
public static void write(byte[] data, Writer output) throws IOException
Writes bytes from abyte[]
to chars on aWriter
using the default character encoding of the platform. This method usesString.String(byte[])
.
- Parameters:
data
- the byte array to write, do not modify during output, null ignoredoutput
- theWriter
to write to
- Since:
- Commons IO 1.1
public static void write(byte[] data, Writer output, String encoding) throws IOException
Writes bytes from abyte[]
to chars on aWriter
using the specified character encoding. Character encoding names can be found at IANA. This method usesString.String(byte[], String)
.
- Parameters:
data
- the byte array to write, do not modify during output, null ignoredoutput
- theWriter
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(char[] data, OutputStream output) throws IOException
Writes chars from achar[]
to bytes on anOutputStream
. This method usesString.String(char[])
andString.getBytes()
.
- Parameters:
data
- the char array to write, do not modify during output, null ignoredoutput
- theOutputStream
to write to
- Since:
- Commons IO 1.1
public static void write(char[] data, OutputStream output, String encoding) throws IOException
Writes chars from achar[]
to bytes on anOutputStream
using the specified character encoding. Character encoding names can be found at IANA. This method usesString.String(char[])
andString.getBytes(String)
.
- Parameters:
data
- the char array to write, do not modify during output, null ignoredoutput
- theOutputStream
to write toencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(char[] data, Writer output) throws IOException
Writes chars from achar[]
to aWriter
using the default character encoding of the platform.
- Parameters:
data
- the char array to write, do not modify during output, null ignoredoutput
- theWriter
to write to
- Since:
- Commons IO 1.1
public static void writeLines(Collection lines, String lineEnding, OutputStream output) throws IOException
Writes thetoString()
value of each item in a collection to anOutputStream
line by line, using the default character encoding of the platform and the specified line ending.
- Parameters:
lines
- the lines to write, null entries produce blank lineslineEnding
- the line separator to use, null is system defaultoutput
- theOutputStream
to write to, not null, not closed
- Since:
- Commons IO 1.1
public static void writeLines(Collection lines, String lineEnding, OutputStream output, String encoding) throws IOException
Writes thetoString()
value of each item in a collection to anOutputStream
line by line, using the specified character encoding and the specified line ending. Character encoding names can be found at IANA.
- Parameters:
lines
- the lines to write, null entries produce blank lineslineEnding
- the line separator to use, null is system defaultoutput
- theOutputStream
to write to, not null, not closedencoding
- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void writeLines(Collection lines, String lineEnding, Writer writer) throws IOException
Writes thetoString()
value of each item in a collection to aWriter
line by line, using the specified line ending.
- Parameters:
lines
- the lines to write, null entries produce blank lineslineEnding
- the line separator to use, null is system defaultwriter
- theWriter
to write to, not null, not closed
- Since:
- Commons IO 1.1