最近因为写一个东西,要把配置保存在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();
}
}