Java BufferedReader to String loadText(BufferedReader reader)

Here you can find the source of loadText(BufferedReader reader)

Description

Loads the given BufferedReader as text, line by line into a String .

License

Open Source License

Parameter

Parameter Description
reader a parameter

Exception

Parameter Description
IOException an exception

Return

reader contents as string

Declaration

public static String loadText(BufferedReader reader) throws IOException 

Method Source Code

//package com.java2s;
/*//  ww w  . j  av a  2  s  .c  om
 *   __               .__       .__  ._____.           
 * _/  |_  _______  __|__| ____ |  | |__\_ |__   ______
 * \   __\/  _ \  \/  /  |/ ___\|  | |  || __ \ /  ___/
 *  |  | (  <_> >    <|  \  \___|  |_|  || \_\ \\___ \ 
 *  |__|  \____/__/\_ \__|\___  >____/__||___  /____  >
 *                   \/       \/             \/     \/ 
 *
 * Copyright (c) 2006-2011 Karsten Schmidt
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

import java.io.*;

public class Main {
    /**
     * Loads the given {@link BufferedReader} as text, line by line into a
     * {@link String}.
     * 
     * @param reader
     * @return reader contents as string
     * @throws IOException
     */
    public static String loadText(BufferedReader reader) throws IOException {
        StringBuilder result = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line).append('\n');
        }
        return result.toString();
    }

    /**
     * Loads the given {@link InputStream} as text, line by line into a
     * {@link String} using UTF-8 encoding.
     * 
     * @param input
     *            stream
     * @return stream contents as string
     * @throws IOException
     */
    public static String loadText(InputStream input) throws IOException {
        return loadText(input, "UTF-8");
    }

    /**
     * Loads the given {@link InputStream} as text, line by line into a
     * {@link String} using the specified encoding.
     * 
     * @param input
     *            stream
     * @param encoding
     * @return stream contents as single string
     * @throws IOException
     */
    public static String loadText(InputStream input, String encoding) throws IOException {
        byte[] raw = loadBytes(input);
        return new String(raw, encoding);
    }

    /**
     * Loads the given {@link InputStream} into a byte array buffer.
     * 
     * @param stream
     * @return byte array
     * @throws IOException
     */
    static public byte[] loadBytes(InputStream stream) throws IOException {
        BufferedInputStream input = new BufferedInputStream(stream);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int c;
        while ((c = input.read()) != -1) {
            buffer.write(c);
        }
        return buffer.toByteArray();
    }
}

Related

  1. bufferToString(BufferedReader br)