Java Utililty Methods Resource Get

List of utility methods to do Resource Get

Description

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

Method

FilegetResourceFile(Class clazz, String fileName)
get Resource File
ClassLoader classLoader = clazz.getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null)
    throw new FileNotFoundException(fileName + " not found");
return new File(resource.toURI());
FilegetResourceFile(String name)
Returns a file handle for the resource with the given name.
URI uri = Thread.currentThread().getContextClassLoader().getResource(name).toURI();
return new File(uri);
URLgetResourceFromBundle(Class clazz, String location)
get Resource From Bundle
Objects.requireNonNull(clazz);
Objects.requireNonNull(location);
return FrameworkUtil.getBundle(clazz).getResource(location);
URLgetResourceFromFile(String filename)
get Resource From File
URL url = null;
File file = new File(filename);
if (file.exists()) {
    try {
        url = file.toURL();
    } catch (MalformedURLException e) {
        return null;
return url;
URLgetResourceInBundleOrFragments(Bundle bundle, String name)
get Resource In Bundle Or Fragments
String dirName = "/";
String fileName = name;
int index = name.lastIndexOf('/');
if (index > 0) {
    dirName = name.substring(0, index);
    fileName = name.substring(index + 1);
} else if (index == 0) {
    fileName = name.substring(1);
...
ListgetResourceListing(ClassLoader cl)
get Resource Listing
URL dirURL = cl.getResource("");
if (dirURL != null && dirURL.getProtocol().equals("file")) {
    File f = new File(dirURL.toURI());
    return recurse(f, f.getAbsolutePath().replace('\\', '/'));
if (dirURL.getProtocol().equals("jar")) {
    String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
...
StringgetResourceMessage(Locale locale, String resource, String key, Object... params)
get Resource Message
ResourceBundle bundle = ResourceBundle.getBundle(resource, locale);
String msg = bundle.getString(key);
if (params != null && params.length > 0)
    msg = MessageFormat.format(msg, params);
return msg;
ReadergetResourceReader(Class clazz, String name, String charset)
get Resource Reader
return new InputStreamReader(getResourceStream(clazz, name), charset);
FilegetResources(String archiveName)
get Resources
String name = archiveName;
URL url = Thread.currentThread().getContextClassLoader().getResource(name);
if (url == null) {
    System.err.println("No resources for " + archiveName);
    return null;
try {
    File file = new File(url.toURI());
...
ListgetResources(String pkgName)
get Resources
List<URL> urls = new ArrayList<URL>();
try {
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    String path = pkgName.replace('.', '/');
    Enumeration<URL> resources = cld.getResources(path);
...