---
title: "根据文件名获得properties文件"
author: "waynelewis"
date: "2011-07-08T15:05:44Z"
tags: []
categories: ["编程语言"]
slug: "properties"
draft: true
lastmod: "2011-07-08T15:11:33Z"
id: 1119672
url: "https://waynelewis.iteye.com/blog/1119672"
---

直接上代码：

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

```