Java BufferedReader Create getReaderText(BufferedReader reader)

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

Description

get Reader Text

License

Apache License

Declaration

public static String getReaderText(BufferedReader reader) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w w w.j av  a  2 s  .  c  o m*/
 * Copyright 2013-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.Closeable;

import java.io.IOException;

public class Main {
    public static String getReaderText(BufferedReader reader) throws IOException {
        StringBuilder answer = new StringBuilder();
        // reading the content of the file within a char buffer
        // allow to keep the correct line endings
        char[] charBuffer = new char[8192];
        int nbCharRead /* = 0 */;
        try {
            while ((nbCharRead = reader.read(charBuffer)) != -1) {
                // appends buffer
                answer.append(charBuffer, 0, nbCharRead);
            }
        } finally {
            closeWithWarning(reader);
            reader = null;
        }
        return answer.toString();
    }

    public static void closeWithWarning(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                //LOG.warning("Caught exception during close(): " + e);
            }
        }
    }
}

Related

  1. getReader(String Path)
  2. getReader(String path)
  3. getReader(String path, String charEncoding)
  4. getReader(String path, String charEncoding)
  5. getReaderAsString(Reader reader)
  6. getStreamReader(InputStream input)
  7. getStreamText(InputStream stream)
  8. toBufferedReader(final Reader reader)
  9. toBufferedReader(InputStream is, String charset)