Here you can find the source of getResourceAsFile(Class> clazz, String name)
Parameter | Description |
---|---|
clazz | the class with which the resource is associated |
name | the desired resource |
Parameter | Description |
---|---|
FileNotFoundException | if the resource cannot be found |
public static File getResourceAsFile(Class<?> clazz, String name) throws FileNotFoundException
//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()); } }