@Beta public final class CharStreams extends Object
All method parameters must be non-null unless documented otherwise.
Some of the methods in this class take arguments with a generic type of Readable & Closeable
. A Reader
implements both of those interfaces. Similarly for Appendable & Closeable
and Writer
.
Modifier and Type | Method and Description |
---|---|
static Writer |
asWriter(Appendable
Returns a Writer that sends all output to the given
Appendable target.
|
static long |
copy(Readable
Copies all characters between the
Readable and
Appendable objects.
|
static Writer |
nullWriter()
Returns a
Writer that simply discards written chars.
|
static List |
readLines(Readable
Reads all of the lines from a
Readable object.
|
static <T> T |
readLines(Readable
Streams lines from a
Readable object, stopping when the processor returns
false or all lines have been read and returning the result produced by the processor.
|
static void |
skipFully(Reader
Discards
n characters of data from the reader.
|
static String |
toString(Readable
|
public static long copy(Readablefrom, Appendable to) throws IOException
Readable
and
Appendable
objects. Does not close or flush either object.
from
- the object to read from
to
- the object to write to
IOException
- if an I/O error occurs
public static StringtoString(Readable r) throws IOException
r
- the object to read from
IOException
- if an I/O error occurs
public static List<String > readLines(Readable r) throws IOException
Readable
object. The lines do not include line-termination characters, but do include other leading and trailing whitespace.
Does not close the Readable
. If reading files or resources you should use the Files
and Resources
methods.
r
- the object to read from
List
containing all the lines
IOException
- if an I/O error occurs
public static <T> T readLines(Readablereadable, LineProcessor <T> processor) throws IOException
Readable
object, stopping when the processor returns
false
or all lines have been read and returning the result produced by the processor. Does not close
readable
. Note that this method may not fully consume the contents of
readable
if the processor stops processing early.
IOException
- if an I/O error occurs
public static void skipFully(Readerreader, long n) throws IOException
n
characters of data from the reader. This method will block until the full amount has been skipped. Does not close the reader.
reader
- the reader to read from
n
- the number of characters to skip
EOFException
- if this stream reaches the end before skipping all the characters
IOException
- if an I/O error occurs
public static WriternullWriter()
Writer
that simply discards written chars.
public static WriterasWriter(Appendable target)
Appendable
target. Closing the writer will close the target if it is
Closeable
, and flushing the writer will flush the target if it is
Flushable
.
target
- the object to which output will be sent