Package com.google.common.io
Class CharStreams
- java.lang.Object
-
- com.google.common.io.CharStreams
-
@GwtIncompatible public final class CharStreams extends Object
Provides utility methods for working with character streams.Some of the methods in this class take arguments with a generic type of
Readable & Closeable
. AReader
implements both of those interfaces. Similarly forAppendable & Closeable
andWriter
.- Since:
- 1.0
- Author:
- Chris Nokleberg, Bin Zhu, Colin Decker
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Writer
asWriter(Appendable target)
Returns a Writer that sends all output to the givenAppendable
target.static long
copy(Readable from, Appendable to)
Copies all characters between theReadable
andAppendable
objects.static long
exhaust(Readable readable)
Reads and discards data from the givenReadable
until the end of the stream is reached.static Writer
nullWriter()
Returns aWriter
that simply discards written chars.static List<String>
readLines(Readable r)
Reads all of the lines from aReadable
object.static <T extends @Nullable Object>
TreadLines(Readable readable, LineProcessor<T> processor)
Streams lines from aReadable
object, stopping when the processor returnsfalse
or all lines have been read and returning the result produced by the processor.static void
skipFully(Reader reader, long n)
Discardsn
characters of data from the reader.static String
toString(Readable r)
-
-
-
Method Detail
-
copy
@CanIgnoreReturnValue public static long copy(Readable from, Appendable to) throws IOException
Copies all characters between theReadable
andAppendable
objects. Does not close or flush either object.- Parameters:
from
- the object to read fromto
- the object to write to- Returns:
- the number of characters copied
- Throws:
IOException
- if an I/O error occurs
-
toString
public static String toString(Readable r) throws IOException
- Parameters:
r
- the object to read from- Returns:
- a string containing all the characters
- Throws:
IOException
- if an I/O error occurs
-
readLines
@Beta public static List<String> readLines(Readable r) throws IOException
Reads all of the lines from aReadable
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 theFiles.readLines(java.io.File, java.nio.charset.Charset)
andResources.readLines(java.net.URL, java.nio.charset.Charset, com.google.common.io.LineProcessor<T>)
methods.- Parameters:
r
- the object to read from- Returns:
- a mutable
List
containing all the lines - Throws:
IOException
- if an I/O error occurs
-
readLines
@Beta @CanIgnoreReturnValue public static <T extends @Nullable Object> T readLines(Readable readable, LineProcessor<T> processor) throws IOException
Streams lines from aReadable
object, stopping when the processor returnsfalse
or all lines have been read and returning the result produced by the processor. Does not closereadable
. Note that this method may not fully consume the contents ofreadable
if the processor stops processing early.- Throws:
IOException
- if an I/O error occurs- Since:
- 14.0
-
exhaust
@Beta @CanIgnoreReturnValue public static long exhaust(Readable readable) throws IOException
Reads and discards data from the givenReadable
until the end of the stream is reached. Returns the total number of chars read. Does not close the stream.- Throws:
IOException
- Since:
- 20.0
-
skipFully
@Beta public static void skipFully(Reader reader, long n) throws IOException
Discardsn
characters of data from the reader. This method will block until the full amount has been skipped. Does not close the reader.- Parameters:
reader
- the reader to read fromn
- the number of characters to skip- Throws:
EOFException
- if this stream reaches the end before skipping all the charactersIOException
- if an I/O error occurs
-
nullWriter
@Beta public static Writer nullWriter()
Returns aWriter
that simply discards written chars.- Since:
- 15.0
-
asWriter
@Beta public static Writer asWriter(Appendable target)
Returns a Writer that sends all output to the givenAppendable
target. Closing the writer will close the target if it isCloseable
, and flushing the writer will flush the target if it isFlushable
.- Parameters:
target
- the object to which output will be sent- Returns:
- a new Writer object, unless target is a Writer, in which case the target is returned
-
-