Java Reader Read getReaderContents(Reader reader)

Here you can find the source of getReaderContents(Reader reader)

Description

Reads all the characters into a String.

License

Apache License

Parameter

Parameter Description
reader The Reader to read.

Exception

Parameter Description
IOException If there is an error reading the stream.

Return

The contents of the input stream as a String

Declaration

public static String getReaderContents(Reader reader) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**//from w  w  w.  ja va2 s  .com
     * Reads all the characters into a String.
     *
     * @param reader The Reader to read.
     * @return The contents of the input stream as a String
     * @throws IOException If there is an error reading the stream.
     */
    public static String getReaderContents(Reader reader) throws IOException {
        try {
            StringBuffer result = new StringBuffer();

            char[] buffer = new char[2048];
            int read;
            while ((read = reader.read(buffer)) > -1) {
                result.append(buffer, 0, read);
            }
            return result.toString();
        } finally {
            closeQuietly(reader);
        }
    }

    public static void closeQuietly(Reader input) {
        closeQuietly((Closeable) input);
    }

    public static void closeQuietly(InputStream input) {
        closeQuietly((Closeable) input);
    }

    public static void closeQuietly(Closeable input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}

Related

  1. getReaderAsString(Reader source)
  2. getReaderContentAsString(Reader reader)
  3. load(InputStream input, int bufsize)
  4. loadChars(Reader in)
  5. loadFully(String path)
  6. loadReader(final Reader in, final StringBuffer buffer)