zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Redis总结
    设计模式-单例模式
    spring5源码解读
    关于asp.net MVC 中的TryUpdateModel方法
    WebSocket在ASP.NET MVC4中的简单实现 (该文章转自网络,经尝试并未实现,请大神指点。)
    C# 新特性 dynamic的使用及扩展
    C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)
    C#通用类型转换 Convert.ChangeType 转自网络
    浅谈.NET反射机制的性能优化 转自网络 博客园
    浅析大型网站的架构 转自软件中国
  • 原文地址:https://www.cnblogs.com/zhonghuazhi/p/3593151.html
Copyright © 2011-2022 走看看