Java ByteArrayOutputStream Write readAllTextFromResource(String resourceName)

Here you can find the source of readAllTextFromResource(String resourceName)

Description

Reads the entire resource text file.

License

Apache License

Declaration


public static String readAllTextFromResource(String resourceName) 

Method Source Code

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

import java.io.*;

public class Main {
    /**//from   w w w. j  a v  a  2 s .c  om
     * Reads the entire resource text file.
     * * @param resourceName Name of the resource text file
     * * @return Content of the resource text file.
     */

    public static String readAllTextFromResource(String resourceName) {
        String s = "";
        ;
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
        try {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            s = result.toString("UTF-8");
        } catch (Exception e) {
            // Omitted.
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // Omitted.
            }
        }
        return s;
    }
}

Related

  1. readAllAndKeepOpen(InputStream bytes, int bufferSize)
  2. readAllBinary(InputStream stream)
  3. readAllFrom(InputStream in, boolean chunked)
  4. readAllFromFile(File f)
  5. readAllFromFile(String file)