java中找到资源文件的路径

类别:Java 点击:0 评论:0 推荐:
最近因为写一个东西,要把配置保存在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();
        }     }
 

本文地址:http://com.8s8s.com/it/it14827.htm