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

PropertiesgetResource(final String resource)
Reads configuration from a classpath resource stream obtained from the current thread's class loader through ClassLoader#getSystemResourceAsStream(String) .
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (input == null) {
    throw new IllegalArgumentException("configuration file '" + resource + "' not found on classpath");
final Properties config = new Properties();
try {
    config.load(input);
} catch (final IOException e) {
...
FilegetResource(String fileName)
get Resource
return new File(attemptResolvePathFromBSearchRoot("resources/" + fileName));
FilegetResource(String fileName, int type)
Returns a game resource.
String filePath;
switch (type) {
default:
    filePath = paths.get(type) + fileName;
    break;
case RES_PREFERENCE:
    filePath = paths.get(type) + fileName + ".properties";
    break;
...
InputStreamgetResource(String name)
get Resource
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(name);
return is;
InputStreamgetResource(String path)
Get a resource relative to the application class.
InputStream x = ClassLoader.getSystemResourceAsStream("./" + path);
if (x != null)
    return x;
try {
    return new FileInputStream("./data/" + path);
} catch (FileNotFoundException e) {
    e.printStackTrace();
return null;
InputStreamgetResource(String propertiesPath)
get Resource
return new FileInputStream(new File(propertiesPath));
InputStreamgetResource(String uri)
Get an InputStream from specified resource uri.
try {
    return new URL(uri).openStream();
} catch (MalformedURLException mue) {
    return new FileInputStream(uri);
StringgetResourceAbsolutePath(String pluginId, String... path)
Provide bundle resource absolute path
StringBuilder builder = new StringBuilder();
for (String fragment : path) {
    builder.append("/" + fragment);
String filePath = "";
try {
    filePath = FileLocator.toFileURL(Platform.getBundle(pluginId).getEntry("/")).getFile() + "resources"
            + builder.toString();
...
BufferedReadergetResourceAsBufferedReader(Class clazz, String resourceName)
get Resource As Buffered Reader
return new BufferedReader(getResourceAsInputStreamReader(clazz, resourceName));
byte[]getResourceAsByteArray(Object context, String resourceName)
get Resource As Byte Array
InputStream inputStream = getResourceAsStream(context, resourceName);
final ByteArrayOutputStream fileBytes = new ByteArrayOutputStream();
try {
    writeInputToOutput(inputStream, fileBytes);
} finally {
    inputStream.close();
return fileBytes.toByteArray();
...