Java Utililty Methods Resource File

List of utility methods to do Resource File

Description

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

Method

voidgetEntriesFromResourceBundle(String rbName, Map map)
This method loads the resource bundle & puts all the values in map
ResourceBundle rb = ResourceBundle.getBundle(rbName);
for (Enumeration e = rb.getKeys(); e.hasMoreElements();) {
    String key = (String) e.nextElement();
    map.put(key, rb.getString(key));
FilegetFile(Class clazz, String resource)
Get a file handle for the class's resource.
File result = null;
if (clazz != null) {
    try {
        final URL url = clazz.getResource(resource);
        result = new File(url.toURI());
    } catch (URISyntaxException eat) {
if (result == null) {
    final String filename = getFilename(clazz, resource);
    result = filename == null ? null : getFile(filename);
return result;
FilegetFile(Class resourceClass, String fileName)
Returns a File named fileName associated with Class resourceClass .
try {
    return new File(URLDecoder.decode(resourceClass.getResource(fileName).getFile(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
    return new File(resourceClass.getResource(fileName).getFile());
FilegetFile(Class cls, String resource)
get File
URL url = cls.getResource(resource);
if (url == null)
    return null;
return new File(url.getFile());
FilegetFile(String resource)
Multi protocol resource loader.
File directFile = new File(resource);
if (directFile.exists()) {
    return directFile;
URL classLoader = Thread.currentThread().getContextClassLoader().getResource(resource);
if (classLoader == null) {
    throw new FileNotFoundException("Unable to locate '" + resource + "' as direct File or on classpath");
String fromURL = classLoader.getFile();
if (fromURL == null || fromURL.isEmpty()) {
    throw new FileNotFoundException("Unable to convert URL '" + fromURL + "' to File");
return new File(fromURL);
FilegetFile(String resourceOrFile, Class cls, boolean deleteTmpOnExit)
Use getInputStream instead, because Files are trouble, when in (maven) JARs
try {
    URI uri = getURL(resourceOrFile, cls).toURI();
    String uriStr = uri.toString();
    if (uriStr.startsWith("jar")) {
        if (uriStr.endsWith("/")) {
            throw new UnsupportedOperationException("cannot unjar directories, only files");
        String jarPath = uriStr.substring(4, uriStr.indexOf("!")).replace("file:", "");
...
FilegetFileByResources(String fileName, Class clazz)
get File By Resources
String strFile = clazz.getResource(fileName).getFile();
return new File(URLDecoder.decode(strFile, "utf-8"));
FilegetFileFromRelativeResource(final Class type, final String relativeResourceName)
Returns the specified file from a resource relative to the given type.
final URL url = type.getResource(relativeResourceName);
final File file = getFileFrom(url);
return file;
FilegetFileFromSystemResources(String fileName)
get File From System Resources
File file = null;
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
if (classLoader != null) {
    URL url = classLoader.getResource(fileName);
    if (url == null) {
        url = classLoader.getResource(File.separator + fileName);
    try {
...
StringgetFilename(Class clazz, String resource)
Get the filename for the class's resource suitable for opening.
String filename = null;
if (clazz == null) {
    filename = resource;
} else {
    final URL url = getUrl(clazz, resource);
    if (url != null) {
        filename = url.toString();
return filename;