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.
| Modifier and Type | Field 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. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static void | closeQuietly(Reader
the Reader to close, may be null or already closed input)Unconditionally close an |
| public static void | closeQuietly(Writer
the Writer to close, may be null or already closed output)Unconditionally close a |
| public static void | closeQuietly(InputStream
the InputStream to close, may be null or already closed input)Unconditionally close an |
| public static void | closeQuietly(OutputStream
the OutputStream to close, may be null or already closed output)Unconditionally close an |
| public static boolean | Returns: true if the content of the streams are equal or they both don't exist, false otherwisethe 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 otherwisethe 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 copiedthe input, OutputStream InputStream to read fromthe output)OutputStream to write toCopy bytes from an |
| public static void | copy(InputStream
the input, Writer InputStream to read fromthe output)Writer to write toCopy bytes from an |
| public static void | copy(InputStream
the input, Writer InputStream to read fromthe output, String Writer to write tothe encoding to use, null means platform default encoding)Copy bytes from an |
| public static int | |
| public static void | copy(Reader
the input, OutputStream Reader to read fromthe output)OutputStream to write toCopy chars from a |
| public static void | copy(Reader
the input, OutputStream Reader to read fromthe output, String OutputStream to write tothe encoding to use, null means platform default encoding)Copy chars from a |
| public static long | Returns: the number of bytes copiedthe input, OutputStream InputStream to read fromthe output)OutputStream to write toCopy bytes from a large (over 2GB) |
| public static long | |
| public static LineIterator | Returns: an Iterator of the lines in the reader, never nullthe reader)Reader to read from, not nullReturn an Iterator for the lines in a |
| public static LineIterator | Returns: an Iterator of the lines in the reader, never nullthe input, String InputStream to read from, not nullthe encoding to use, null means platform default encoding)Return an Iterator for the lines in an |
| public static List | Returns: the list of Strings, never nullthe input)InputStream to read from, not nullGet the contents of an |
| public static List | Returns: the list of Strings, never nullthe input, String InputStream to read from, not nullthe encoding to use, null means platform default encoding)Get the contents of an |
| public static List | |
| public static byte[] | Returns: the requested byte arraythe input)InputStream to read fromGet the contents of an |
| public static byte[] | Returns: the requested byte arraythe input)Reader to read fromGet the contents of a |
| public static byte[] | Returns: the requested byte arraythe input, String Reader to read fromthe encoding to use, null means platform default encoding)Get the contents of a |
| public static byte[] | Returns: the requested byte arraythe input)String to convert
Deprecated
Use
Get the contents of a String#getBytes()
String as a byte[]
using the default character encoding of the platform.
|
| public static char[] | Returns: the requested character arraythe is)InputStream to read fromGet the contents of an |
| public static char[] | Returns: the requested character arraythe is, String InputStream to read fromthe encoding to use, null means platform default encoding)Get the contents of an |
| public static char[] | Returns: the requested character arraythe input)Reader to read fromGet the contents of a |
| public static InputStream | Returns: an input streamthe 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 streamthe 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 Stringthe input)InputStream to read fromGet the contents of an |
| public static String | Returns: the requested Stringthe input, String InputStream to read fromthe encoding to use, null means platform default encoding)Get the contents of an |
| public static String | |
| public static String | Returns: the requested Stringthe byte array to read from input)
Deprecated
Use
Get the contents of a String#String(byte[])
byte[] as a String
using the default character encoding of the platform.
|
| public static String | Returns: the requested Stringthe byte array to read from input, String the encoding to use, null means platform default encoding)
Deprecated
Use
Get the contents of a String#String(byte[], String)
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 output)OutputStream to write toWrites bytes from a |
| public static void | |
| public static void | |
| public static void | |
| public static void | write(char[]
the char array to write, do not modify during output,
null ignored data, OutputStream the output)OutputStream to write toWrites chars from a |
| public static void | write(char[]
the char array to write, do not modify during output,
null ignored data, OutputStream the output, String OutputStream to write tothe encoding to use, null means platform default encoding)Writes chars from a |
| public static void | |
| public static void | write(String
the data, OutputStream String to write, null ignoredthe output)OutputStream to write toWrites chars from a |
| public static void | write(String
the data, OutputStream String to write, null ignoredthe output, String OutputStream to write tothe encoding to use, null means platform default encoding)Writes chars from a |
| public static void | write(StringBuffer
the data, Writer StringBuffer to write, null ignoredthe output)Writer to write toWrites chars from a |
| public static void | write(StringBuffer
the data, OutputStream StringBuffer to write, null ignoredthe output)OutputStream to write toWrites chars from a |
| public static void | write(StringBuffer
the data, OutputStream StringBuffer to write, null ignoredthe output, String OutputStream to write tothe encoding to use, null means platform default encoding)Writes chars from a |
| 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 output)OutputStream to write to, not null, not closedWrites the |
| 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 output, String OutputStream to write to, not null, not closedthe encoding to use, null means platform default encoding)Writes the |
| 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)Writer to write to, not null, not closedWrites the |