• JAVA 获取系统环境变量


    分享代码:

     1 package com.base.entity;
     2 
     3 import java.io.Serializable;
     4 import java.util.Comparator;
     5 
     6 /**
     7  * 系统环境变量
     8  * 
     9  * @author test
    10  * @create 2014-3-10下午04:35:47
    11  * @version 1.0
    12  */
    13 public class SystemProperty implements Serializable, Comparator {
    14 
    15     private static final long serialVersionUID = 1L;
    16 
    17     // 属性key
    18     private String iKey;
    19 
    20     // 属性Value
    21     private String iVal;
    22 
    23     public SystemProperty() {
    24         // TODO Auto-generated constructor stub
    25     }
    26 
    27     public SystemProperty(String iKey, String iVal) {
    28         super();
    29         this.iKey = iKey;
    30         this.iVal = iVal;
    31     }
    32 
    33     @Override
    34     public int compare(Object o1, Object o2) {
    35         // TODO Auto-generated method stub
    36 
    37         SystemProperty s1 = (SystemProperty) o1;
    38         SystemProperty s2 = (SystemProperty) o2;
    39 
    40         return s1.getiKey().compareTo(s2.getiKey());
    41     }
    42 
    43     public String getiKey() {
    44         return iKey;
    45     }
    46 
    47     public void setiKey(String iKey) {
    48         this.iKey = iKey;
    49     }
    50 
    51     public String getiVal() {
    52         return iVal;
    53     }
    54 
    55     public void setiVal(String iVal) {
    56         this.iVal = iVal;
    57     }
    58 }
     1 package com.util.common;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.Iterator;
     6 import java.util.List;
     7 import java.util.Map;
     8 
     9 import com.base.entity.SystemProperty;
    10 
    11 /**
    12  * @author test
    13  * @create 2014-3-10下午04:39:08
    14  * @version 1.0
    15  */
    16 public final class Read {
    17 
    18     /**
    19      * 获取系统所有环境变量
    20      * 
    21      * @return List<SystemProperty>
    22      * @throws Exception
    23      */
    24     public static List<SystemProperty> readSysPropertyAll() throws Exception {
    25         return readSysProperty("BOCO_ALL");
    26     }
    27 
    28     /**
    29      * 获取系统指定环境变量
    30      * 
    31      * @param key
    32      *            环境变量Key
    33      * @return List<SystemProperty>
    34      * @throws Exception
    35      */
    36     public static List<SystemProperty> readSysProperty(String key)
    37             throws Exception {
    38 
    39         List<SystemProperty> list = new ArrayList<SystemProperty>();
    40 
    41         Map m = System.getenv();
    42         String keys = "";
    43         if (key.equals("BOCO_ALL")) {
    44             for (Iterator<String> iter = m.keySet().iterator(); iter.hasNext();) {
    45                 keys = iter.next();
    46                 list.add(new SystemProperty(keys, m.get(keys).toString()));
    47                 Collections.sort(list, new SystemProperty());
    48             }
    49         } else {
    50             if (m.containsKey(key)) {
    51                 list.add(new SystemProperty(key, m.get(key).toString()));
    52             } else {
    53                 throw new Exception("系统中未包含指定Key:" + key);
    54             }
    55         }
    56         return list;
    57     }
    58 }
  • 相关阅读:
    Ubuntu18.04下更改apt源为阿里云源
    sudo: pip:command not found问题解决
    CentOS8 中文输入法
    PyCharm2019 激活方式
    正则爬取京东商品信息并打包成.exe可执行程序
    学习springcloud bus+springcloud config实现刷新配置
    使用springcloud-config统一管理配置文件
    记录一下今天使用maven构建项目分多个模块时遇到的扫描properties问题
    spring+mybatis+thymeleaf
    回调地狱问题
  • 原文地址:https://www.cnblogs.com/zhonghuazhi/p/3593151.html
Copyright © 2020-2023  润新知