我的mail.properties文件放在WEB-INF目录下,我如何得到他的路径下面是方法片段/** *//**
     * 初始化Configuration类
     * @param filePath 要读取的配置文件的路径+名称
     */
    public Configuration(String filePath)
    {
        propertie = new Properties();
        try {
            inputFile = new FileInputStream(filePath);
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) {
            System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在"+filePath.toString());
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("装载文件--->失败!");
            ex.printStackTrace();
        }
    }//end ReadConfigInfo()如何获取filePath用了getContextClassLoader().getResource("").getPath()等方法的的不到,希望大家帮帮忙,jdk1.6

解决方案 »

  1.   

    this.getClass().getClassLoader().getResource("").getPath();
    用这个试试看
      

  2.   

    貌似web-inf下面的文件是不给访问的,建议放在src下面
      

  3.   

    properties.load(PropertiesOper.class.getClassLoader().getResourceAsStream("com/main.properties"));
      

  4.   

    PropertiesOper 换成你的类名
      

  5.   

    要看你的properties文件放在哪里了。
    要是在.class文件夹下就用类加载器
    要是在web应用下就用servletContext.getrealPath(文件相对于web应用根目录的相对url)
      

  6.   

    如果你吧配置文件放在SRC下面,也就是与java在同级,可以这样来获取:Properties prop=new Properties();
    Inputstream instream= getClass().getResourceAsStream("sql.properties");
    prop.load(instream);
      

  7.   

    URL url = getServlet().getServletContext().getResource("/WEB-INF/web.xml");
    ///localhost/loa/WEB-INF/web.xml
    System.out.println(url.getPath());
    URLConnection conn = url.openConnection();
    conn.connect();
    InputStream input = conn.getInputStream();