
Java工具类---------获取系统环境变量以及配置文件的值
发布日期:2021-05-06 19:32:57
浏览次数:18
分类:精选文章
本文共 1462 字,大约阅读时间需要 4 分钟。
我们在开发时 有很多配置|配置文件需要放在环境变量中 以便于多个项目|不同开发者 或等等原因的使用
Java为我们内置了读取环境变量的方法 这里加以简单的封装package com.hqjl.career.util;import com.hqjl.common.util.Common;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;/** * @author chunying */public class ComfigPropertyUtil { public static String getHQJLHome () { // System.getenv() 是获取名为env的系统环境变量 String path = System.getenv(“env”).replace('\\', '/'); // 一般是开发自己的电脑 如果没有设置环境变量 可以自己自定义一个路径 方便开发 if (path == null) { path = "d:/env"; } return path; } public static MapgetConfigMap(){ //获取环境变量路径下 某个具体的配置文件 String combinationConfigPath = getHQJLHome() + "/config/assess/exploreOuter.properties"; Map map = new HashMap (); try { //将配置文件的值通过reader流读入 一个Map BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(new File(combinationConfigPath)), "utf-8")); String line = null; while ((line = br.readLine()) != null) { String[] split = line.split("="); map.put(split[0], split[1]); } br.close(); } catch (Exception e) { System.err.println(e.getMessage()); } return map; }}当我们使用的时候 直接调用getConfigMap() 获取到一个map Map的key 就是 配置等号左边的值 直接get即可
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年05月07日 03时19分12秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
learn c++(vector and array)
2023-01-30
Learning English With Our Team
2023-01-30
Learning jQuery, 4th Edition 勘误表
2023-01-30
Learning Perl 学习笔记
2023-01-30
Learning Python 008 正则表达式-001
2023-01-30
Learning to act by predicting the future
2023-01-30
Learning XNA 4.0 第三章(结尾)
2023-01-30
Leedcode12-word-break-i
2023-01-30
Leedcode2-后缀表达式结果
2023-01-30
Leedcode3- Max Points on a Line 共线点个数
2023-01-30
Leedcode4-sort listnode 归并排序
2023-01-30
Leedcode6-binary-tree-preorder-traversal
2023-01-30
Leedcode7-binary-tree-postorder-traversal
2023-01-30
Leedcode8-reorder-list
2023-01-30
Leedcode9-linked-list-cycle-i
2023-01-30
LeetCode #9 Palindrome Number
2023-01-30
LeetCode - 48. Rotate Image
2023-01-30
LeetCode - 50. Pow(x, n)
2023-01-30