Java Utililty Methods Resource Load

List of utility methods to do Resource Load

Description

The list of methods to do Resource Load are organized into topic(s).

Method

ListgetFilesRecurse(File dir, final String pattern)
get Files Recurse
return getFilesRecurse(dir, pattern, null);
ListgetFilesRecursevely(final String sourceFolder, final String classRegEx)
get Files Recursevely
return getFilesRecursevely(sourceFolder, classRegEx, true);
StringgetResource(Class c, String name)
get Resource
InputStream is = c.getResourceAsStream(name);
StringBuilder text = new StringBuilder();
Scanner scanner = new Scanner(is, "UTF-8");
try {
    while (scanner.hasNextLine()) {
        text.append(scanner.nextLine());
        text.append("\n");
} finally {
    scanner.close();
return text.toString();
StringgetResource(Class c, String name)
Reads a string resource from the resource path (=classpath) of the given class.
InputStream is = c.getResourceAsStream(name);
return getResource(is);
InputStreamgetResource(Class c, String path)
get Resource
return c.getResourceAsStream(path);
StringgetResource(Class clazz, String fileName)
get Resource
try {
    File file = new File(clazz.getResource(fileName).getFile());
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
    StringBuilder builder = new StringBuilder();
    String str;
    while ((str = in.readLine()) != null) {
        builder.append(str);
    in.close();
    return builder.toString();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
return null;
StringgetResource(Class clazz, String name, String charset)
get Resource
InputStream in = clazz.getResourceAsStream(name);
try {
    return readerToString(new InputStreamReader(in, charset));
} finally {
    in.close();
InputStreamgetResource(Class c, String name)
Similar to Class#getResourceAsStream(String) except looks up the parent hierarchy for the existence of the specified resource.
if (name == null)
    return null;
while (c != null) {
    InputStream is = c.getResourceAsStream(name);
    if (is != null)
        return is;
    c = c.getSuperclass();
return null;
InputStreamgetResource(ClassLoader classLoader, final String path)
get Resource
InputStream in = classLoader.getResourceAsStream(path);
if (in == null) {
    throw new IOException("Empty resource.");
return in;
InputStreamgetResource(final String aResName)
Loads a resource from the classpath.
return Thread.currentThread().getContextClassLoader().getResourceAsStream(aResName);