Java BufferedReader Read All readAllText(InputStream stream)

Here you can find the source of readAllText(InputStream stream)

Description

Reads all text from the stream using the default charset.

License

Open Source License

Declaration

public static String readAllText(InputStream stream) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String readAllText(File file) throws IOException {
        InputStream stream = new FileInputStream(file);
        try {//w w w  . j a  v  a  2  s  . c om
            return readAllText(stream);
        } finally {
            stream.close();
        }
    }

    /**
     * Reads all text from the stream using the default charset. Does not close
     * the stream.
     */
    public static String readAllText(InputStream stream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        StringBuilder result = new StringBuilder();
        while (true) {
            String s = reader.readLine();
            if (s == null) {
                return result.toString();
            }
            result.append(s);
            result.append('\n');
        }
    }
}

Related

  1. readAllStreamFromClasspathBaseResource(Class resourceBase, String dataLocation)
  2. readAllStreamsFromClasspathBaseResource(Class resourceBase, String[] dataLocations)
  3. readAllText(File file)
  4. readAllText(File file)
  5. readAllText(final InputStream inputStream)
  6. readAllText(String filePath)
  7. readAllText(String path)
  8. readAllTextFromSystemResource(String path)