Java BufferedReader Read All readAllStreamsFromClasspathBaseResource(Class resourceBase, String[] dataLocations)

Here you can find the source of readAllStreamsFromClasspathBaseResource(Class resourceBase, String[] dataLocations)

Description

read All Streams From Classpath Base Resource

License

Apache License

Declaration

public static List<String> readAllStreamsFromClasspathBaseResource(Class<?> resourceBase,
            String[] dataLocations) 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;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> readAllStreamsFromClasspathBaseResource(Class<?> resourceBase,
            String[] dataLocations) throws IOException {

        final List<String> scriptContent = new ArrayList<String>();

        for (int i = 0; i < dataLocations.length; i++) {
            String content = readAllStreamFromClasspathBaseResource(resourceBase, dataLocations[i]);

            if (content != null) {
                scriptContent.add(content);
            }//from   ww w  .  j av  a 2  s. co  m
        }

        return scriptContent;
    }

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

        if (isFileAvailableOnClasspath(resourceBase, dataLocation)) {
            return readFullStream(resourceBase.getResourceAsStream(dataLocation));
        } else {
            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(InputStream inputStream)
  3. readAllLines(InputStream is)
  4. readAllLines(String filepath)
  5. readAllStreamFromClasspathBaseResource(Class resourceBase, String dataLocation)
  6. readAllText(File file)
  7. readAllText(File file)
  8. readAllText(final InputStream inputStream)
  9. readAllText(InputStream stream)