Java Resource File toFile(Class c, String resource)

Here you can find the source of toFile(Class c, String resource)

Description

Converts the URL of the specified resource into an abstract pathname.

License

Open Source License

Parameter

Parameter Description
c class to be used to find the resource
resource name of the desired resource

Exception

Parameter Description
IllegalArgumentException if the URL of the specified resource does not adhere to the<code>file</code> scheme

Return

the abstract pathname denoted by the specified resource

Declaration

public static File toFile(Class c, String resource) 

Method Source Code


//package com.java2s;
/*//from ww  w.  j  ava2  s.  c  o m
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
 * published by JBoss Inc.; either version 1.0 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.io.File;

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

public class Main {
    /**
     * Converts the URL of the specified resource into an abstract pathname.
     * @param c class to be used to find the resource
     * @param resource name of the desired resource
     * @return the abstract pathname denoted by the specified resource
     * @throws IllegalArgumentException if the URL of the specified resource does not adhere to the
     * <code>file</code> scheme
     */
    public static File toFile(Class c, String resource) {
        URL resourceUrl = c.getResource(resource);
        if (resourceUrl == null)
            return null;
        try {
            return new File(new URI(resourceUrl.toExternalForm()));
        } catch (URISyntaxException e) {
            // should not happen
            throw new AssertionError(e);
        }
    }
}

Related

  1. printUsage(ResourceBundle bundle)
  2. put(String resource, Map headers, String data)
  3. resourceExists(String s)
  4. runtimeResourceLocation(String src)
  5. sanitizeResource(String inputResource)
  6. toStream(String resource)
  7. unpackDeps(final String resource, final ClassLoader cl)