| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Objectorg.apache.commons.io.IOUtilspublic class IOUtilsextends java.lang.ObjectBufferedInputStream
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 IOExceptionCompare the contents of two Streams to determine if they are equal or not. This method buffers the input internally usingBufferedInputStreamif 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 IOExceptionCompare the contents of two Readers to determine if they are equal or not. This method buffers the input internally usingBufferedReaderif 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 IOExceptionCopy bytes from anInputStreamto 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-1after 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- theInputStreamto read fromoutput- theOutputStreamto write to
- Returns:
- the number of bytes copied
- Since:
- Commons IO 1.1
public static void copy(InputStream input,
Writer output)
throws IOExceptionCopy bytes from anInputStreamto chars on aWriterusing 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- theInputStreamto read fromoutput- theWriterto write to
- Since:
- Commons IO 1.1
public static void copy(InputStream input,
Writer output,
String encoding)
throws IOExceptionCopy bytes from anInputStreamto chars on aWriterusing 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- theInputStreamto read fromoutput- theWriterto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void copy(Reader input,
OutputStream output)
throws IOExceptionCopy chars from aReaderto bytes on anOutputStreamusing 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- theReaderto read fromoutput- theOutputStreamto write to
- Since:
- Commons IO 1.1
public static void copy(Reader input,
OutputStream output,
String encoding)
throws IOExceptionCopy chars from aReaderto bytes on anOutputStreamusing 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- theReaderto read fromoutput- theOutputStreamto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static int copy(Reader input,
Writer output)
throws IOExceptionCopy chars from aReaderto 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-1after 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- theReaderto read fromoutput- theWriterto write to
- Returns:
- the number of characters copied
- Since:
- Commons IO 1.1
public static long copyLarge(InputStream input,
OutputStream output)
throws IOExceptionCopy bytes from a large (over 2GB)InputStreamto anOutputStream. This method buffers the input internally, so there is no need to use aBufferedInputStream.
- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to
- Returns:
- the number of bytes copied
- Since:
- Commons IO 1.3
public static long copyLarge(Reader input,
Writer output)
throws IOExceptionCopy chars from a large (over 2GB)Readerto aWriter. This method buffers the input internally, so there is no need to use aBufferedReader.
- Parameters:
input- theReaderto read fromoutput- theWriterto 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).LineIteratorholds a reference to the openInputStreamspecified 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- theInputStreamto 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.LineIteratorholds a reference to the openReaderspecified 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- theReaderto 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 IOExceptionGet the contents of anInputStreamas 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- theInputStreamto 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 IOExceptionGet the contents of anInputStreamas 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- theInputStreamto 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 IOExceptionGet the contents of aReaderas a list of Strings, one entry per line. This method buffers the input internally, so there is no need to use aBufferedReader.
- Parameters:
input- theReaderto read from, not null
- Returns:
- the list of Strings, never null
- Since:
- Commons IO 1.1
public static byte[] toByteArray(InputStream input)
throws IOExceptionGet the contents of anInputStreamas abyte[]. This method buffers the input internally, so there is no need to use aBufferedInputStream.
- Parameters:
input- theInputStreamto read from
- Returns:
- the requested byte array
public static byte[] toByteArray(Reader input)
throws IOExceptionGet the contents of aReaderas 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- theReaderto read from
- Returns:
- the requested byte array
public static byte[] toByteArray(Reader input,
String encoding)
throws IOExceptionGet the contents of aReaderas 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- theReaderto 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 IOExceptionDeprecated. Use
String.getBytes()Get the contents of aStringas abyte[]using the default character encoding of the platform. This is the same asString.getBytes().
- Parameters:
input- theStringto convert
- Returns:
- the requested byte array
public static char[] toCharArray(InputStream is)
throws IOExceptionGet the contents of anInputStreamas 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- theInputStreamto read from
- Returns:
- the requested character array
- Since:
- Commons IO 1.1
public static char[] toCharArray(InputStream is,
String encoding)
throws IOExceptionGet the contents of anInputStreamas 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- theInputStreamto 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 IOExceptionGet the contents of aReaderas a character array. This method buffers the input internally, so there is no need to use aBufferedReader.
- Parameters:
input- theReaderto 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 IOExceptionConvert 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 IOExceptionGet the contents of anInputStreamas 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- theInputStreamto read from
- Returns:
- the requested String
public static String toString(InputStream input,
String encoding)
throws IOExceptionGet the contents of anInputStreamas 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- theInputStreamto read fromencoding- the encoding to use, null means platform default
- Returns:
- the requested String
public static String toString(Reader input)
throws IOExceptionGet the contents of aReaderas a String. This method buffers the input internally, so there is no need to use aBufferedReader.
- Parameters:
input- theReaderto read from
- Returns:
- the requested String
public static String toString(byte[] input)
throws IOExceptionDeprecated. 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 IOExceptionDeprecated. 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 IOExceptionWrites chars from aStringto bytes on anOutputStreamusing the default character encoding of the platform. This method usesString.getBytes().
- Parameters:
data- theStringto write, null ignoredoutput- theOutputStreamto write to
- Since:
- Commons IO 1.1
public static void write(String data,
OutputStream output,
String encoding)
throws IOExceptionWrites chars from aStringto bytes on anOutputStreamusing the specified character encoding. Character encoding names can be found at IANA. This method usesString.getBytes(String).
- Parameters:
data- theStringto write, null ignoredoutput- theOutputStreamto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(String data,
Writer output)
throws IOExceptionWrites chars from aStringto aWriter.
- Parameters:
data- theStringto write, null ignoredoutput- theWriterto write to
- Since:
- Commons IO 1.1
public static void write(StringBuffer data,
OutputStream output)
throws IOExceptionWrites chars from aStringBufferto bytes on anOutputStreamusing the default character encoding of the platform. This method usesString.getBytes().
- Parameters:
data- theStringBufferto write, null ignoredoutput- theOutputStreamto write to
- Since:
- Commons IO 1.1
public static void write(StringBuffer data,
OutputStream output,
String encoding)
throws IOExceptionWrites chars from aStringBufferto bytes on anOutputStreamusing the specified character encoding. Character encoding names can be found at IANA. This method usesString.getBytes(String).
- Parameters:
data- theStringBufferto write, null ignoredoutput- theOutputStreamto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(StringBuffer data,
Writer output)
throws IOExceptionWrites chars from aStringBufferto aWriter.
- Parameters:
data- theStringBufferto write, null ignoredoutput- theWriterto write to
- Since:
- Commons IO 1.1
public static void write(byte[] data,
OutputStream output)
throws IOExceptionWrites bytes from abyte[]to anOutputStream.
- Parameters:
data- the byte array to write, do not modify during output, null ignoredoutput- theOutputStreamto write to
- Since:
- Commons IO 1.1
public static void write(byte[] data,
Writer output)
throws IOExceptionWrites bytes from abyte[]to chars on aWriterusing 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- theWriterto write to
- Since:
- Commons IO 1.1
public static void write(byte[] data,
Writer output,
String encoding)
throws IOExceptionWrites bytes from abyte[]to chars on aWriterusing 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- theWriterto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(char[] data,
OutputStream output)
throws IOExceptionWrites 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- theOutputStreamto write to
- Since:
- Commons IO 1.1
public static void write(char[] data,
OutputStream output,
String encoding)
throws IOExceptionWrites chars from achar[]to bytes on anOutputStreamusing 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- theOutputStreamto write toencoding- the encoding to use, null means platform default
- Since:
- Commons IO 1.1
public static void write(char[] data,
Writer output)
throws IOExceptionWrites chars from achar[]to aWriterusing the default character encoding of the platform.
- Parameters:
data- the char array to write, do not modify during output, null ignoredoutput- theWriterto write to
- Since:
- Commons IO 1.1
public static void writeLines(Collection lines,
String lineEnding,
OutputStream output)
throws IOExceptionWrites thetoString()value of each item in a collection to anOutputStreamline 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- theOutputStreamto write to, not null, not closed
- Since:
- Commons IO 1.1
public static void writeLines(Collection lines,
String lineEnding,
OutputStream output,
String encoding)
throws IOExceptionWrites thetoString()value of each item in a collection to anOutputStreamline 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- theOutputStreamto 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 IOExceptionWrites thetoString()value of each item in a collection to aWriterline 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- theWriterto write to, not null, not closed
- Since:
- Commons IO 1.1