Search in sources :

Example 76 with Writer

use of java.io.Writer in project guava by google.

the class CharSink method writeFrom.

/**
   * Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink.
   * Does not close {@code readable} if it is {@code Closeable}.
   *
   * @return the number of characters written
   * @throws IOException if an I/O error occurs while reading from {@code readable} or writing to
   *     this sink
   */
@CanIgnoreReturnValue
public long writeFrom(Readable readable) throws IOException {
    checkNotNull(readable);
    Closer closer = Closer.create();
    try {
        Writer out = closer.register(openStream());
        long written = CharStreams.copy(readable, out);
        // https://code.google.com/p/guava-libraries/issues/detail?id=1330
        out.flush();
        return written;
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 77 with Writer

use of java.io.Writer in project guava by google.

the class CharSource method copyTo.

/**
   * Copies the contents of this source to the given sink.
   *
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs while reading from this source or writing to
   *     {@code sink}
   */
@CanIgnoreReturnValue
public long copyTo(CharSink sink) throws IOException {
    checkNotNull(sink);
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        Writer writer = closer.register(sink.openStream());
        return CharStreams.copy(reader, writer);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Writer(java.io.Writer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 78 with Writer

use of java.io.Writer in project guava by google.

the class AppendableWriterTest method testAppendMethods.

public void testAppendMethods() throws IOException {
    StringBuilder builder = new StringBuilder();
    Writer writer = new AppendableWriter(builder);
    writer.append("Hello,");
    writer.append(' ');
    writer.append("The World Wide Web", 4, 9);
    writer.append("!");
    assertEquals("Hello, World!", builder.toString());
}
Also used : Writer(java.io.Writer)

Example 79 with Writer

use of java.io.Writer in project guava by google.

the class CharSinkTest method testOpenBufferedStream.

public void testOpenBufferedStream() throws IOException {
    Writer writer = sink.openBufferedStream();
    assertTrue(sink.wasStreamOpened());
    assertFalse(sink.wasStreamClosed());
    writer.write(STRING);
    writer.close();
    assertTrue(sink.wasStreamClosed());
    assertEquals(STRING, sink.getString());
}
Also used : Writer(java.io.Writer)

Example 80 with Writer

use of java.io.Writer in project guava by google.

the class AppendableWriterTest method testCloseFlush.

public void testCloseFlush() throws IOException {
    SpyAppendable spy = new SpyAppendable();
    Writer writer = new AppendableWriter(spy);
    writer.write("Hello");
    assertFalse(spy.flushed);
    assertFalse(spy.closed);
    writer.flush();
    assertTrue(spy.flushed);
    assertFalse(spy.closed);
    writer.close();
    assertTrue(spy.flushed);
    assertTrue(spy.closed);
}
Also used : Writer(java.io.Writer)

Aggregations

Writer (java.io.Writer)1410 OutputStreamWriter (java.io.OutputStreamWriter)554 IOException (java.io.IOException)474 StringWriter (java.io.StringWriter)333 File (java.io.File)306 FileOutputStream (java.io.FileOutputStream)213 BufferedWriter (java.io.BufferedWriter)202 FileWriter (java.io.FileWriter)196 PrintWriter (java.io.PrintWriter)175 OutputStream (java.io.OutputStream)139 Test (org.junit.Test)120 InputStreamReader (java.io.InputStreamReader)81 Reader (java.io.Reader)74 BufferedReader (java.io.BufferedReader)72 ByteArrayOutputStream (java.io.ByteArrayOutputStream)70 ArrayList (java.util.ArrayList)66 HashMap (java.util.HashMap)66 Map (java.util.Map)65 InputStream (java.io.InputStream)61 Properties (java.util.Properties)40