欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

根据文件名获得properties文件

程序员文章站 2022-05-10 09:11:42
...
直接上代码:
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