Class CharStreams
- Since:
- 1.0
- Author:
- Chris Nokleberg, Bin Zhu, Colin Decker
-
Method Summary
Modifier and TypeMethodDescriptionstatic WriterasWriter(Appendable target) Returns a Writer that sends all output to the givenAppendabletarget.static longcopy(Readable from, Appendable to) Copies all characters between theReadableandAppendableobjects.static longReads and discards data from the givenReadableuntil the end of the stream is reached.static WriterReturns aWriterthat simply discards written chars.Reads all of the lines from aReadableobject.readLines(Readable readable, LineProcessor<T> processor) Streams lines from aReadableobject, stopping when the processor returnsfalseor all lines have been read and returning the result produced by the processor.static voidDiscardsncharacters of data from the reader.static String
-
Method Details
-
copy
Copies all characters between theReadableandAppendableobjects. 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
Reads all characters from aReadableobject into aString. Does not close theReadable.Java 25+ users: If the input is a
Reader, preferReader.readAllAsString().- Parameters:
r- the object to read from- Returns:
- a string containing all the characters
- Throws:
IOException- if an I/O error occurs
-
readLines
Reads all of the lines from aReadableobject. 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(File, Charset)andResources.readLines(URL, Charset, LineProcessor)methods.This method prioritizes convenience over performance: It reads the entire input into memory immediately. To instead read and process lines individually, use an alternative like
BufferedReader.lines().Java 25+ users: If the input is a
Reader, you may preferReader.readAllLines(). The two may have different performance characteristics for differentReaderclasses and Java versions, but both methods read the entire input into memory immediately, so we discourage use of both when memory usage is a concern.- Parameters:
r- the object to read from- Returns:
- a mutable
Listcontaining all the lines - Throws:
IOException- if an I/O error occurs
-
readLines
@CanIgnoreReturnValue public static <T extends @Nullable Object> T readLines(Readable readable, LineProcessor<T> processor) throws IOException Streams lines from aReadableobject, stopping when the processor returnsfalseor 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 ofreadableif the processor stops processing early.Users who can use
Stream: If your input is aReader, consider wrapping it with aBufferedReaderand operating on theStreamof lines returned byBufferedReader.lines().- Throws:
IOException- if an I/O error occurs- Since:
- 14.0
-
exhaust
Reads and discards data from the givenReadableuntil 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
Discardsncharacters 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
Returns aWriterthat simply discards written chars.Java 11+ users: use
Writer.nullWriter()instead. Note that theCharStreamsmethod returns a singleton writer whoseclosemethod has no effect, while theWriter.nullWriter()method returns a new instance whose methods throw after the instance isclosed.- Since:
- 15.0
-
asWriter
Returns a Writer that sends all output to the givenAppendabletarget. 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
-