Java BufferedReader Read All readAllStreamFromClasspathBaseResource(Class resourceBase, String dataLocation)

Here you can find the source of readAllStreamFromClasspathBaseResource(Class resourceBase, String dataLocation)

Description

read All Stream From Classpath Base Resource

License

Apache License

Declaration

public static String readAllStreamFromClasspathBaseResource(Class<?> resourceBase, String dataLocation)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

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

public class Main {
    public static String readAllStreamFromClasspathBaseResource(Class<?> resourceBase, String dataLocation)
            throws IOException {

        if (isFileAvailableOnClasspath(resourceBase, dataLocation)) {
            return readFullStream(resourceBase.getResourceAsStream(dataLocation));
        } else {/*  w ww  .j  a  v a 2 s.  co m*/
            return null;
        }

    }

    public static boolean isFileAvailableOnClasspath(Class<?> resourceBase, String dataLocation) {
        return resourceBase.getResourceAsStream(dataLocation) != null;
    }

    public static String readFullStream(InputStream data) throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data, "UTF-8"));

        StringBuilder readData = new StringBuilder();
        String readLine;

        while ((readLine = bufferedReader.readLine()) != null) {
            readData.append(readLine);
        }

        return readData.toString();
    }
}

Related

  1. readAllLines(File file)
  2. readAllLines(File file)
  3. readAllLines(InputStream inputStream)
  4. readAllLines(InputStream is)
  5. readAllLines(String filepath)
  6. readAllStreamsFromClasspathBaseResource(Class resourceBase, String[] dataLocations)
  7. readAllText(File file)
  8. readAllText(File file)
  9. readAllText(final InputStream inputStream)