读properties文件时 写绝对路径/workProject/firststep/WebRoot/WEB-INF/classes/upload.properties
可以获得文件
如何写相对路径获得
菜鸟问题 别笑话 谢指教

解决方案 »

  1.   

    只要写upload.properties就可以了,但必须要在类路径下,也就是在src目录下
      

  2.   

    我的是web项目 最后一定要部署到WEB-INF/classes/下
    不过我问题解决了 方法比较笨而已 还是要谢谢你
      

  3.   

    //获得classes目录的绝对路径
    String path = getClass().getClassLoader().getResource("/").getPath();
    System.out.println(path);
    //加上你要的文件名
    String filePath=path+"upload.properties";
    System.out.println(filePath);
      

  4.   


    public String getPara(String fileName,String field){
    Properties prop = new Properties();
    try{
    ClassLoader cl = this.getClass().getClassLoader();
    InputStream is = cl.getResourceAsStream(fileName);
    prop.load(is);
    if(is != null) 
    is.close();
    }catch(Exception e){
    System.out.println(e + "file " + fileName + " not found");
    }
    return prop.getProperty(field);
    } public static void main(String[] args){
    ReadPro pro = new ReadPro();
    System.out.println(pro.getPara("upload.properties","要显示的属性"));
    System.out.println(pro.getPara("upload.properties","要显示的属性"));
    }
      

  5.   

    你这样写 要把属性文件放到何处 应该放到classpath中吧 
      

  6.   

    public String loadProp() {
        String separator = System.getProperty("file.separator");
        return "." + separator + "route.properties";
    }
    把属性文件route.properties放在项目根目录底下就可以了。