⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testaa.java

📁 读取用户自定义环境变量
💻 JAVA
字号:
package test;

import java.util.Properties;
import java.io.*;
import java.util.Iterator;

public class TestAA {
  public static Properties getEnvVars() {
    Process p = null;
    Properties envVars = new Properties();
    try {
      Runtime r = Runtime.getRuntime();
      String OS = System.getProperty("os.name").toLowerCase();
      // System.out.println(OS);
      if (OS.indexOf("windows 9") > -1) {
        p = r.exec("command.com /c set");
      }
      else if ( (OS.indexOf("nt") > -1)
               || (OS.indexOf("windows 2000") > -1
                   || (OS.indexOf("windows xp") > -1))) {
        // thanks to JuanFran for the xp fix!
        p = r.exec("cmd.exe /c set");
      }
      else {
        // our last hope, we assume Unix (thanks to H. Ware for the fix)
        p = r.exec("env");
      }
      BufferedReader br = new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ( (line = br.readLine()) != null) {
        int idx = line.indexOf('=');
        String key = line.substring(0, idx);
        String value = line.substring(idx + 1);
        envVars.setProperty(key, value);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
//	 we do not care here. Just no env vars for the user. Sorry.
    }
    return envVars;
  }
  public static void main(String args[]){
    Properties test=getEnvVars();
    if(test!=null){
      Iterator it = test.keySet().iterator();
//      Iterator it = test.values().iterator();
      while(it.hasNext()){
        System.out.println("===="+it.next().toString());
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -