Java Resource Load getResource(Class clazz, String name, String charset)

Here you can find the source of getResource(Class clazz, String name, String charset)

Description

get Resource

License

Apache License

Declaration

public static String getResource(Class clazz, String name, String charset) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * Copyright (c) 2011- kotemaru@kotemaru.org
 ******************************************************************************/

import java.io.*;

public class Main {
    public static String getResource(Class clazz, String name) throws IOException {
        InputStream in = clazz.getResourceAsStream(name);
        try {/*w  w  w. jav a 2  s.  c  o  m*/
            return readerToString(new InputStreamReader(in, "UTF-8"));
        } finally {
            in.close();
        }
    }

    public static String getResource(Class clazz, String name, String charset) throws IOException {
        InputStream in = clazz.getResourceAsStream(name);
        try {
            return readerToString(new InputStreamReader(in, charset));
        } finally {
            in.close();
        }
    }

    public static String readerToString(Reader reader) throws IOException {
        StringBuffer sbuf = new StringBuffer();
        int n = 0;
        char[] buff = new char[4096];
        while ((n = reader.read(buff)) > 0) {
            sbuf.append(buff, 0, n);
        }
        return sbuf.toString();
    }
}

Related

  1. getFilesRecursevely(final String sourceFolder, final String classRegEx)
  2. getResource(Class c, String name)
  3. getResource(Class c, String name)
  4. getResource(Class c, String path)
  5. getResource(Class clazz, String fileName)
  6. getResource(Class c, String name)
  7. getResource(ClassLoader classLoader, final String path)
  8. getResource(final String aResName)
  9. getResource(final String resource)