zoukankan      html  css  js  c++  java
  • Java程序读取tomcat下的properties配置文件

    代码如下:

    //找到tomcat/etc/wx文件夹
       private static String getPropFolderPath()
       {
     /* Properties p = System.getProperties();
      p.list(System.out);*/
      
    //获取tomcat的路径
         String path = System.getProperty("catalina.home");
         if (StringUtils.isEmpty(path)) {
           path = QueryQRCodeUrlController.class.getResource("/").getPath();
           path = new File(path).getParent() + File.separator;
         } else {
           path = path + File.separator + TOMCAT_PATH;
         }
         return path;
       }
    //***********************初始化配置信息************************
    static{
    Properties p=new Properties();
    InputStream in=null;
    try {
    in=new FileInputStream(getPropFolderPath()+WX_PROPERTIES);
    p.load(in);
       VALUE = p.getProperty("wx.serviceId");
       PWD = p.getProperty("wx.pwd");
    } catch (Exception e) {
    logger.error(e.toString());
    }finally{
    if(in!=null){
    try {
    in.close();
    } catch (IOException e) {
    logger.error(e.toString());
    }
    }
    }
    }

    1:  File.separator   与系统有关的默认名称分隔符。

           

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

    比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
    File file1 = new File ("C: mp est.txt");
    在Linux下则是这样的:
    File file2 = new File ("/tmp/test.txt");

    如果要考虑跨平台,则最好是这么写:
    File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");

    File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。

     2: System.getProperties():该方法是获取当前系统的很多属性名和value值。得到这些键值对之后,可以通过下面方法获取value值:

     System.getProperty("catalina.home");《如果清楚系统的这些属性之后,可以直接通过该方法获取相应的value值》

    3: 静态代码块:

    static{

       System.out.println(); 

     用于初始化一些信息,服务启动之后,类被加载时执行。只执行一次。

  • 相关阅读:
    swarm集群搭建 及集群维护操作
    zabbix 告警说明
    yum 下载安装包
    mongdb常见操作
    cloudera5.16.1 离线安装
    centos7 安装hadoop-3.2.1
    rpcbind 启动报错
    ingress-nginx 安装
    Dubbo学习
    mybatis防止SQL注入
  • 原文地址:https://www.cnblogs.com/vanl/p/4873543.html
Copyright © 2011-2022 走看看