Here you can find the source of locateFile(String name)
Parameter | Description |
---|---|
name | the name of the resource - usually a file name (absolute or relative) |
public static URL locateFile(String name) throws FileNotFoundException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.net.URL; import java.io.File; import java.io.IOException; import java.io.FileNotFoundException; public class Main { /********************************************************************* * Return the location of the specified resource by searching the * current directory, user home directory, the current classpath * and the system classpath. <p> */*from ww w . j ava2 s .c o m*/ * The code was inspired by Apache Commons class * <tt>ConfigurationUtils</tt> (authors Herve Quiroz, Oliver * Heger, and Emmanuel Bourg). <p> * * @param name the name of the resource - usually a file name * (absolute or relative) * * @return a URL of the resource location * * @throw FileNotFoundException if the resource cannot be found; * or if the 'name' is null or otherwise invalid ********************************************************************/ public static URL locateFile(String name) throws FileNotFoundException { if (name == null) throw new FileNotFoundException("Null file name."); // attempt to create a URL directly try { return new URL(name); } catch (IOException e) { // let's make another attempt } // attempt to load from an absolute path, or from the current directory File file = new File(name); if (file.isAbsolute() || file.exists()) { try { return file.toURI().toURL(); } catch (IOException e) { throw new FileNotFoundException("Malformed path: " + name); } } // attempt to load from the user home directory try { StringBuilder fName = new StringBuilder(); fName.append(System.getProperty("user.home")); fName.append(File.separator); fName.append(name); file = new File(fName.toString()); if (file.exists()) return file.toURI().toURL(); } catch (IOException e) { // let's make another attempt } // attempt to load from the context classpath ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL url = loader.getResource(name); if (url != null) return url; // attempt to load from the system classpath url = ClassLoader.getSystemResource(name); if (url != null) return url; throw new FileNotFoundException(name); } }