`

Java读取.properties文件

    博客分类:
  • Java
 
阅读更多

1.获取src/config目录下的配置文件

      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/sms.properties");
      Properties p = new Properties();
      // 把配置文件载入Properties类
      p.load(inputStream); 
      String serviceUrl = p.getProperty("serviceUrl");
      //System.out.println(p.getProperty("serviceUrl"));

 

 

2.获取指定WebRoot/classes目录下的配置文件

        Properties p = new Properties();
        try {
            String path = this.getClass().getResource("/").getPath();//得到工程名WEB-INF/classes/路径
            path = path.substring(1, path.indexOf("classes"));//从路径字符串中取出工程路径
            p.load(new FileInputStream(path + "/config/sms.properties"));
            String lzfx_url = p.getProperty("LZFX_URL");
        } catch (Exception e) {
            e.printStackTrace();
        }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics