Top Description Constructors Methods
com.google.common.io

public abstract Class CharSink

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.io.ByteSink.AsCharSink
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

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:

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.

Author
Colin Decker
Since
14.0

Constructor Summary

AccessConstructor and Description
protected
CharSink()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public Writer
openBufferedStream()

Opens a new buffered Writer for writing to this sink.

public abstract Writer
openStream()

Opens a new Writer for writing to this sink.

public void
write(CharSequence charSequence)

Writes the given character sequence to this sink.

public long

Returns:

the number of characters written
writeFrom
(Readable readable)

Writes all the text from the given Readable (such as a Reader) to this sink.

public void
writeLines(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.

public void
writeLines(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.

public void
writeLines(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.

public void
writeLines(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.

private void
writeLines(Iterator<? extends CharSequence> lines, String lineSeparator)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CharSinkback to summary
protected CharSink()

Constructor for use by subclasses.

Method Detail

openBufferedStreamback to summary
public Writer openBufferedStream() throws IOException

Opens a new buffered Writer for writing to this sink. The returned stream is not required to be a BufferedWriter in order to allow implementations to simply delegate to openStream() 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.

Exceptions
IOException:
if an I/O error occurs while opening the writer
Since
15.0 (in 14.0 with return type BufferedWriter)
openStreamback to summary
public abstract Writer openStream() throws IOException

Opens a new Writer for 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.

Exceptions
IOException:
if an I/O error occurs while opening the writer
writeback to summary
public void write(CharSequence charSequence) throws IOException

Writes the given character sequence to this sink.

Exceptions
IOException:
if an I/O error while writing to this sink
writeFromback to summary
public long writeFrom(Readable readable) throws IOException

Writes all the text from the given Readable (such as a Reader) to this sink. Does not close readable if it is Closeable.

Returns:long

the number of characters written

Annotations
@CanIgnoreReturnValue
Exceptions
IOException:
if an I/O error occurs while reading from readable or writing to this sink
writeLinesback to summary
public void writeLines(Iterable<? extends CharSequence> lines) throws IOException

Writes 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 to writeLines(lines, System.getProperty("line.separator")).

Exceptions
IOException:
if an I/O error occurs while writing to this sink
writeLinesback to summary
public 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.

Exceptions
IOException:
if an I/O error occurs while writing to this sink
writeLinesback to summary
public void writeLines(Stream<? extends CharSequence> lines) throws IOException

Writes 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 to writeLines(lines, System.getProperty("line.separator")).

Exceptions
IOException:
if an I/O error occurs while writing to this sink
Since
22.0
writeLinesback to summary
public 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.

Exceptions
IOException:
if an I/O error occurs while writing to this sink
Since
22.0
writeLinesback to summary
private void writeLines(Iterator<? extends CharSequence> lines, String lineSeparator) throws IOException