Java Reader Read All readAllToString(Reader reader)

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

Description

Reads all from a Reader into a String.

License

Apache License

Parameter

Parameter Description
reader Reader

Exception

Parameter Description
IOException an exception

Return

the String

Declaration

public static String readAllToString(Reader reader) throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.IOException;

import java.io.Reader;

public class Main {
    /**/*from w  w w.  j av a  2  s .co m*/
     * Reads all from a Reader into a String. Close the Reader when finished.
     *
     * @param reader Reader
     * @return the String
     * @throws IOException
     */
    public static String readAllToString(Reader reader) throws IOException {
        char buf[] = new char[4096];
        StringBuilder strBuffer = new StringBuilder();
        int size = 0;
        try {
            while ((size = reader.read(buf)) != -1) {
                strBuffer.append(buf, 0, size);
            }
        } finally {
            reader.close();
        }
        return strBuffer.toString();
    }
}

Related

  1. readAllCharsAndClose(Reader reader)
  2. readAllCharsFromReader(Reader reader)
  3. readAllFrom(final Reader reader)
  4. readAllLines(final BufferedReader br)
  5. readAllQuietly(Reader reader)