Java Resource Path Get getResourcePath(String fileName)

Here you can find the source of getResourcePath(String fileName)

Description

Return the absolute path of the specified file resource on the classpath.

License

Open Source License

Exception

Parameter Description
IOException if a valid path to an existing file cannot be returned

Declaration

private static String getResourcePath(String fileName) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;
import java.io.IOException;

import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**/*from  w w w .  j av a  2 s.  c om*/
     * Return the absolute path of the specified file resource on the classpath.
     * @throws IOException if a valid path to an existing file cannot be returned
     */
    private static String getResourcePath(String fileName) throws IOException {
        URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
        if (url == null) {
            throw new IOException("Failed to locate resource " + fileName);
        }
        File file;
        try {
            file = new File(url.toURI());
        } catch (URISyntaxException urise) {
            throw new IOException("Invalid URL: " + urise);
        }
        String path = file.getAbsolutePath();
        if (!file.exists()) {
            throw new IOException("File " + path + " does not exist");
        }
        return path;
    }
}

Related

  1. getResourceListing(Class clazz, String path, String glob)
  2. getResourcePath()
  3. getResourcePath(Class clazz, String fileName)
  4. getResourcePath(final Class bundleClazz, final String pathToFile)
  5. getResourcePath(Object object, String resource)
  6. getResourcePath(String filename)
  7. getResourcePath(String name)
  8. getResourcePath(String path)
  9. getResourcePath(String resource)