Java BufferedReader Read All readAll(Reader reader)

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

Description

Suck all the data from a Reader into a String .

License

Open Source License

Declaration

public static String readAll(Reader reader) throws IOException 

Method Source Code


//package com.java2s;
/* $This file is distributed under the terms of the license in /doc/license.txt$ */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.Reader;

public class Main {
    /**// w w w .j a va2  s .c o  m
     * Suck all the data from a {@link Reader} into a {@link String}.
     */
    public static String readAll(Reader reader) throws IOException {
        StringBuilder result = new StringBuilder();
        BufferedReader buffered = new BufferedReader(reader);
        char[] chunk = new char[4096];
        int howMany;

        try {
            while (-1 != (howMany = buffered.read(chunk))) {
                result.append(chunk, 0, howMany);
            }
        } finally {
            reader.close();
        }

        return result.toString();
    }
}

Related

  1. readAll(InputStream input)
  2. readAll(InputStream stream)
  3. readAll(Reader reader)
  4. readAll(Reader reader)
  5. readAll(Reader reader)
  6. readAll(String fileName)
  7. readAll(String fileName)
  8. readAll(String filePath)
  9. readAll(String filePathAndName)