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 ...
随机推荐
- windows下编译boost for android
env: windows xp 32 bit mingw official NDK 1. 下载源代码 地址是 :http://sourceforge.net/projects/boost/fi ...
- 《从零开始学Swift》学习笔记(Day 13)——数据类型之整型和浮点型
Swift 2.0学习笔记(Day 13)——数据类型之整型和浮点型 原创文章,欢迎转载.转载请注明:关东升的博客 Swift提供8.16.32.64位形式的有符号及无符号整数.这些整数类型遵循 ...
- FFF at Valentine(强连通分量缩点+拓扑排序)
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- WCF基础之传输
WCF中使用的主要传输的方式有HTTP,TCP和命名管道. 绑定包括可选的协议绑定元素(如安全),必需的编码绑定元素和必须的传输协定绑定元素三个部分,而由传输方式则是由传输绑定元素来决定的. HTTP ...
- Sql注入基础_mysql注入
Mysql数据库结构 数据库A 表名 列名 数据 数据库B 表名 列名 数据 Mysql5.0以上自带数据库:information_schema information_schema:存储mysql ...
- Linux Centos使用脚本安装Docker
1.使用sudo或root权限登录Centos. 2.确保yum包更新到最新. sudo yum update 3.执行Docker安装脚本 # 执行这个脚本会添加docker.repo源并安装Doc ...
- spring 登录提示 Bad credentials
spring 日志输出:Authentication failed: password does not match stored value in spring security 3.2,检查密码发 ...
- windows安装mysql教程2017最新
1.首先在mysql官网下载最新版mysql, 附上链接点击打开链接,根据你的系统型号选择对应的包下载,大约300多兆,版本号为5.7.19 下载完之后,解压缩,是一个标准的mysql文件 2.第二步 ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...