最近因为写一个东西,要把配置保存在XML里,可是如何找到xml哪? 是个问题。看了JWhich,写了个寻找资源文件路径的小函数. (不知道是不是适合所有情况,假如有不对,请指出)
/**
* 假如找不到,则返回null
* @param sResourceName
* @return
*/
public static String getResourceFilePath(String sResourceName)
{
if (!sResourceName.startsWith("/"))
{
sResourceName = "/" + sResourceName;
}
Java.net.URL classUrl = JWhichUtil.class.getResource(sResourceName);
if (classUrl == null)
{
System.out.println("\nResource '" + sResourceName + "' not found in \n'"
+ System.getProperty("java.class.path") + "'");
return null;
}
else
{
System.out.println("\nResource '" + sResourceName + "' found in \n'" + classUrl.getFile() + "'");
return classUrl.getFile();
}
}