Reads characters available from the Reader and returns these characters as a String object. : Reader « File « Java Tutorial






/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Main {
  /**
   * Fully reads the characters available from the supplied Reader
   * and returns these characters as a String object.
   *
   * @param reader The Reader to read the characters from.
   * @return A String existing of the characters that were read.
   * @throws IOException If I/O error occurred.
   */
  public static final String readFully(Reader reader)
    throws IOException
  {
    CharArrayWriter out = new CharArrayWriter(4096);
    transfer(reader, out);
    out.close();

    return out.toString();
  }

  /**
   * Transfers all characters that can be read from <tt>in</tt> to
   * <tt>out</tt>.
   *
   * @param in The Reader to read characters from.
   * @param out The Writer to write characters to.
   * @return The total number of characters transfered.
   */
  public static final long transfer(Reader in, Writer out)
    throws IOException
  {
    long totalChars = 0;
    int charsInBuf = 0;
    char[] buf = new char[4096];

    while ((charsInBuf = in.read(buf)) != -1) {
      out.write(buf, 0, charsInBuf);
      totalChars += charsInBuf;
    }

    return totalChars;
  }
}








11.29.Reader
11.29.1.Read and return the entire contents of the supplied Reader. This method always closes the reader when finished reading.
11.29.2.Read from Reader and write to Writer until there is no more input from reader.
11.29.3.Reads characters available from the Reader and returns these characters as a String object.
11.29.4.Transfers all characters that can be read from one Reader to another Reader
11.29.5.Writes all characters from a Reader to a file using the default character encoding.
11.29.6.convert Reader to InputStream
11.29.7.Compare the contents of two Readers to determine if they are equal or not.
11.29.8.An InputStream backed by a Reader
11.29.9.Reader: Reading Text (Characters)
11.29.10.UTF8 Reader
11.29.11.CRLF Terminated Reader