Java properties配置文件工具类
/*
* Copyright (c) 2017. Panteng.Co.Ltd All rights reserved
*/
import org.apache.log4j.Logger; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; /**
* @author panteng
* @description
* @date 17-2-7.
*/
public class PropertiesUtil {
public static Logger logger = Logger.getLogger(PropertiesUtil.class);
public static Properties pros = new Properties(); static {
// 获取配置文件所在文件夹
String configDir = PropertiesUtil.class.getClassLoader().getResource("").getPath();
// 遍历文件夹下面的所有配置文件
File dir = new File(configDir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().indexOf(".properties") > -1) {
InputStream path = PropertiesUtil.class.getClassLoader().getResourceAsStream(files[i].getName());
try {
pros.load(path);
} catch (IOException e) {
logger.error("{}", e);
}
}
}
}
}
对于打包成的jar包文件,需要读取jar里面的配置文件时,就会出现问题!对应修改如下:
/*
* Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
*/ package com.xiaomi.weather.utils; import org.apache.log4j.Logger; import java.io.*;
import java.util.Properties; /**
* @author panteng
* @description
* @date 17-2-7.
*/
public class PropertiesUtil {
public static Logger logger = Logger.getLogger(PropertiesUtil.class);
public static Properties pros = new Properties(); static {
// 获取配置文件所在文件夹
String configDir = PropertiesUtil.class.getClassLoader().getResource("").getPath();
if (configDir.indexOf(".jar!") > -1) {//jar包
try {
InputStream ips = PropertiesUtil.class.getResourceAsStream("/service.properties");
BufferedReader ipss = new BufferedReader(new InputStreamReader(ips));
pros.load(ipss);
System.out.println("============================XXX" + pros.get("mongoHost"));
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 遍历文件夹下面的所有配置文件
File dir = new File(configDir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().indexOf(".properties") > -1) {
InputStream path = PropertiesUtil.class.getClassLoader().getResourceAsStream(files[i].getName());
try {
pros.load(path);
} catch (IOException e) {
logger.error("{}", e);
}
}
}
}
}
}
获取jar内配置文件(scala):
val regularInputStream = this.getClass.getClassLoader.getResourceAsStream("regular.txt")
val regularBr = new BufferedReader(new InputStreamReader(regularInputStream))
var line: String = null
while ( {
line = regularBr.readLine();
line != null
}) {
val i1 = line.indexOf("\t")
val i2 = line.indexOf("\t", i1 + 1)
val i3 = line.indexOf("\t", i2 + 1)
val i4 = line.indexOf("\t", i3 + 1)
val i5 = line.indexOf("\t", i4 + 1)
descriptions.append(new Regex(line.substring(i4 + 1, i5)))
appIds.append(line.substring(i1 + 1, i2))
titles.append(null)
}
Java properties配置文件工具类的更多相关文章
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- Java加载Properties配置文件工具类
Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...
- 开发读取.properties 配置文件工具类PropertiesUtil
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...
- Java基础-DButils工具类(QueryRunner)详解
Java基础-DButils工具类(QueryRunner)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC ...
- Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...
- Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类
Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类 =========================== ©Copyright 蕃薯耀 2017年9月25日 http://www ...
- java 邮件发送工具类【来源网络自己已经实际应用】
最近在做一个Java发送邮件的工具类,现在分享一下完整的代码 首先需要java邮件的包javax.mail-1.5.4.jar 之前因为链接给错了,很不好意思,现在重新发一次. 包在这里可以下载htt ...
- HttpTool.java(在java tool util工具类中已存在) 暂保留
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...
- java文件处理工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
随机推荐
- cocos2d-x:七彩连珠
大学前,我一直以为学计算机就是学做游戏,或者玩游戏...(我果断的用大学的差不多五分之二实践了后面半块.) 大学后,人们一直以为学计算机的就是 修电脑.装机.盗qq号.word.excel....最近 ...
- cocos2d-x:Layer::setPosition
如果Node的实际类型是Layer或者其派生类, setPosition是不是有猫腻? std::string menuImage = "menu.png"; auto menuI ...
- selenium-webdriver 中执行js代码
#获取标签的text文本值 js1="return document.getElementById('key1').innerText" dr.execute_script(js1 ...
- 巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换
昨日内容回顾: day66 1. 内容回顾 1. 数据的展示 数据通过ORM查询出来 对象列表 QuerySet 1. 普通的字段 对象.字段名 ——> 数据库中的值 2. choices (( ...
- Sql注入_mysql权限入侵
实验:测试不同数据库用户的操作权限 文件读写测试:load_file() ,into outfile 数据库用户账号密码存储在mysql.user下 Mysql最高权限用户root: Mysql普通权 ...
- django startapp报 maximum recursion depth exceeded
报错截图如下: 解决办法:修改指定路径下的functools.py文件的def total_ordering(cls):方法: 原来的样子: convert = { '__lt__': [('__gt ...
- Linux下环境变量配置错误 导致大部分命令不可以使用的解决办法
直接解决方法:在命令行中输入:export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 后 Enter
- app开发学习需要经历哪些流程
app开发学习需要经历哪些流程?如何零基础入门app开发?以下是知乎热心开发者的经验总结,对学习app开发有很好的参考意义 1.如果没有编程基础的,学习基础知识的过程肯定是必须的.2.有了一些基础 ...
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决(转)
通过终端安装程序sudo apt-get install xxx时出错: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource t ...
- 5 Best VPNs for Ubuntu
https://www.bestvpn.com/blog/6268/5-best-vpns-for-ubuntu/?nabe=6412130213429248:0&utm_referrer=h ...