Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I was loading a java resource via the classloader. I had the need to know where the file was on the file system.

    /**
     * Gets the absolute path of a file loaded via a classloader.
     * @param classpathFile file to be loaded from the classpath.
     * @return absolute file path of the file
     * @throws URISyntaxException
     */
    public static String getAbsoluteFilePath(String classpathFile) throws URISyntaxException {
        String absoluteFilePath = "";
        URL url = CommonUtil.class.getResource(classpathFile);
        if (url != null) {
            Path path = Paths.get(url.toURI());
            if (path != null) {
                absoluteFilePath = path.toString();
            }
        }
        return absoluteFilePath;
    }

February 14th, 2014

Posted In: Java, java ninja, Javaninja

Tags: , , , , , , , , ,

Leave a Comment

WP to LinkedIn Auto Publish Powered By : XYZScripts.com