Java InputStreamReader Read readFile(ClassLoader classloader, String filename)

Here you can find the source of readFile(ClassLoader classloader, String filename)

Description

Just for reading a damn file from a package using class loader.

License

Apache License

Parameter

Parameter Description
classloader a class loader to use for loading the file
filename the file name. Package path should be given with slashes.

Return

the contents of the file

Declaration

public static String readFile(ClassLoader classloader, String filename) 

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 {
    /**/* w  w w .j  a v  a2  s  .  c om*/
     * Just for reading a damn file from a package using class loader.
     * 
     * @param classloader a class loader to use for loading the file
     * @param filename the file name. Package path should be given with slashes.
     * @return the contents of the file
     **/
    public static String readFile(ClassLoader classloader, String filename) {
        InputStream istream = classloader.getResourceAsStream(filename);
        if (istream == null)
            return null;

        BufferedReader reader = new BufferedReader(new InputStreamReader(istream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                istream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
}

Related

  1. load(final File file, final Class clazz)
  2. load(String fileName)
  3. loadAsText(InputStream in, String encoding, int bufferSize)
  4. readFile(Class cl, String filename)
  5. readFile(Class cls, String filename)
  6. readFile(File f)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)