Class CharSink
java.lang.Object
com.google.common.io.CharSink
A destination to which characters can be written, such as a text file. Unlike a 
Writer, a
 CharSink is not an open, stateful stream that can be written to and closed. Instead, it
 is an immutable supplier of Writer instances.
 CharSink provides two kinds of methods:
 
- Methods that return a writer: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned writer is closed.
- Convenience methods: These are implementations of common operations that are typically implemented by opening a writer using one of the methods in the first category, doing something and finally closing the writer that was opened.
Any ByteSink may be viewed as a CharSink with a specific character encoding using ByteSink.asCharSink(Charset). Characters written to the
 resulting CharSink will written to the ByteSink as encoded bytes.
- Since:
- 14.0
- Author:
- Colin Decker
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionOpens a new bufferedWriterfor writing to this sink.abstract WriterOpens a newWriterfor writing to this sink.voidwrite(CharSequence charSequence) Writes the given character sequence to this sink.longvoidwriteLines(Iterable<? extends CharSequence> lines) Writes the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator.voidwriteLines(Iterable<? extends CharSequence> lines, String lineSeparator) Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.voidwriteLines(Stream<? extends CharSequence> lines) Writes the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator.voidwriteLines(Stream<? extends CharSequence> lines, String lineSeparator) Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.
- 
Constructor Details- 
CharSinkprotected CharSink()Constructor for use by subclasses.
 
- 
- 
Method Details- 
openStreamOpens a newWriterfor writing to this sink. This method returns a new, independent writer each time it is called.The caller is responsible for ensuring that the returned writer is closed. - Throws:
- IOException- if an I/O error occurs while opening the writer
 
- 
openBufferedStreamOpens a new bufferedWriterfor writing to this sink. The returned stream is not required to be aBufferedWriterin order to allow implementations to simply delegate toopenStream()when the stream returned by that method does not benefit from additional buffering. This method returns a new, independent writer each time it is called.The caller is responsible for ensuring that the returned writer is closed. - Throws:
- IOException- if an I/O error occurs while opening the writer
- Since:
- 15.0 (in 14.0 with return type BufferedWriter)
 
- 
writeWrites the given character sequence to this sink.- Throws:
- IOException- if an I/O error while writing to this sink
 
- 
writeLinesWrites the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator. This method is equivalent towriteLines(lines, System.getProperty("line.separator")).- Throws:
- IOException- if an I/O error occurs while writing to this sink
 
- 
writeLinespublic void writeLines(Iterable<? extends CharSequence> lines, String lineSeparator) throws IOException Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.- Throws:
- IOException- if an I/O error occurs while writing to this sink
 
- 
writeLinesWrites the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator. This method is equivalent towriteLines(lines, System.getProperty("line.separator")).- Throws:
- IOException- if an I/O error occurs while writing to this sink
- Since:
- 33.4.0 (but since 22.0 in the JRE flavor)
 
- 
writeLinespublic void writeLines(Stream<? extends CharSequence> lines, String lineSeparator) throws IOException Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.- Throws:
- IOException- if an I/O error occurs while writing to this sink
- Since:
- 33.4.0 (but since 22.0 in the JRE flavor)
 
- 
writeFromWrites all the text from the givenReadable(such as aReader) to this sink. Does not closereadableif it isCloseable.- Returns:
- the number of characters written
- Throws:
- IOException- if an I/O error occurs while reading from- readableor writing to this sink
 
 
-