java中找到资源文件的路径
时间:2006-04-21 来源:fenge1981
最近因为写一个东西,要把配置保存在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();
} }
* 如果找不到,则返回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();
} }
相关阅读 更多 +