@Beta @GwtIncompatible 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 target)
Returns a Writer that sends all output to the given
Appendable target. |
static long |
copy(Readable from,
Appendable to)
Copies all characters between the
Readable and Appendable objects. |
static long |
exhaust(Readable readable)
Reads and discards data from the given
Readable until the end of the stream is
reached. |
static Writer |
nullWriter()
Returns a
Writer that simply discards written chars. |
static List<String> |
readLines(Readable r)
Reads all of the lines from a
Readable object. |
static <T> T |
readLines(Readable readable,
LineProcessor<T> processor)
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 reader,
long n)
Discards
n characters of data from the reader. |
static String |
toString(Readable r)
|
public static long copy(Readable from, Appendable to) throws IOException
Readable
and Appendable
objects. Does not
close or flush either object.from
- the object to read fromto
- the object to write toIOException
- if an I/O error occurspublic static String toString(Readable r) throws IOException
r
- the object to read fromIOException
- if an I/O error occurspublic 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.readLines(java.io.File, java.nio.charset.Charset)
and Resources.readLines(java.net.URL, java.nio.charset.Charset, com.google.common.io.LineProcessor<T>)
methods.
r
- the object to read fromList
containing all the linesIOException
- if an I/O error occurspublic static <T> T readLines(Readable readable, 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 occurspublic static long exhaust(Readable readable) throws IOException
Readable
until the end of the stream is
reached. Returns the total number of chars read. Does not close the stream.IOException
public static void skipFully(Reader reader, 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 fromn
- the number of characters to skipEOFException
- if this stream reaches the end before skipping all the charactersIOException
- if an I/O error occurspublic static Writer nullWriter()
Writer
that simply discards written chars.public static Writer asWriter(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 sentCopyright © 2010–2017. All rights reserved.