Java Resource Get getResourceAsFile(Class clazz, String name)

Here you can find the source of getResourceAsFile(Class clazz, String name)

Description

Returns a File for the specified resource, associated with the specified class.

License

Open Source License

Parameter

Parameter Description
clazz the class with which the resource is associated
name the desired resource

Exception

Parameter Description
FileNotFoundException if the resource cannot be found

Return

a for the resource, if it is found

Declaration

public static File getResourceAsFile(Class<?> clazz, String name) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*-/*from w  ww.  j  av a 2s.  co m*/
 * Copyright 2012 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

public class Main {
    /**
     * Returns a {@link File} for the specified resource, associated with the
     * specified class.
     * 
     * @param clazz the class with which the resource is associated
     * @param name the desired resource
     * 
     * @return a {@link File} for the resource, if it is found
     * 
     * @throws FileNotFoundException if the resource cannot be found
     */
    public static File getResourceAsFile(Class<?> clazz, String name) throws FileNotFoundException {
        URL url = clazz.getResource(name);
        if (url == null) {
            throw new FileNotFoundException(name + " (resource not found)");
        }
        return new File(url.getFile());
    }
}

Related

  1. getResourceAsBytes(String resource)
  2. getResourceAsFile(Class clazz, String resource)
  3. getResourceAsFile(Class klass, String resource)
  4. getResourceAsFile(Class sourceClass, String reference)
  5. getResourceAsFile(Class clazz, String fn)
  6. getResourceAsFile(Class clazz, String rscName)
  7. getResourceAsFile(final Class clazz, final String name)
  8. getResourceAsFile(final String filename, final Class theClass)
  9. getResourceAsFile(String name)