根据文件名获得properties文件

直接上代码:
package org.commons;
import java.net.URL;

public class PropertiesUtil {
	/**
	 * 
	 * @param path
	 *            文件名
	 * @return 该文件的URL
	 */
	public static URL findAsResource(String path) {

		URL url = null;
		// First, try to locate this resource through the current
		// context classloader.
		ClassLoader contextClassLoader = Thread.currentThread()
				.getContextClassLoader();
		if (contextClassLoader != null) {
			url = contextClassLoader.getResource(path);
		}
		if (url != null)
			return url;

		// Next, try to locate this resource through this class's classloader
		url = PropertiesUtil.class.getClassLoader().getResource(path);
		if (url != null)
			return url;

		// Next, try to locate this resource through the system classloader
		url = ClassLoader.getSystemClassLoader().getResource(path);

		// Anywhere else we should look?
		return url;
	}
}

你可能感兴趣的:(properties)