Here you can find the source of getClassPathFile(final String path, final ClassLoader classLoader)
Parameter | Description |
---|---|
path | Path to File |
classLoader | ClassLoader that should be able to see the file |
public static File getClassPathFile(final String path, final ClassLoader classLoader)
//package com.java2s; /*// w w w.j a v a 2s . co m * Copyright 2009 Tomas Varaneckas * http://www.varaneckas.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.net.URL; public class Main { /** * Gets a {@link File} from a String that represents file path which is * relative to current Class Path * * @param path Path to File * @param classLoader {@link ClassLoader} that should be able to see the * file * @return {@link File} object or null if nothing is found */ public static File getClassPathFile(final String path, final ClassLoader classLoader) { final URL url = classLoader.getResource(path); if (url == null) { return null; } return new File(url.getFile()); } /** * Gets a {@link File} form a String that represents file path which is * relative to current Class Path. Uses system's default ClassLoader. * * @see #getClassPathFile(String, ClassLoader) * @param path Path to File * @return {@link File} object or null if nothing is found */ public static File getClassPathFile(final String path) { return getClassPathFile(path, ClassLoader.getSystemClassLoader()); } }