ITeye博客

根据文件名获得properties文件

waynelewis · 2011-07-08 15:05 · web · 编程语言 · Markdown

直接上代码:

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;
	}
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。