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 ...
随机推荐
- diango-tinymce富文本编译器
应用到项目中 在settings.py中为INSTALLED_APPS添加编辑器应用 INSTALLED_APPS = ( ... 'tinymce', ) 在settings.py中添加编辑配置项 ...
- Unity3d 创建线程 子线程与主线程通信
创建子线程 一,不带参数 Thread resourcesLoadThread=new Thread (this.resourceLoadTxt); resourcesLoadThread.Sta ...
- L - Points on Cycle(旋转公式)
L - Points on Cycle Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- EasyNVR互联网监控直播分发出RTMP、HLS、HTTP-FLV视频流说明介绍
背景需求 需求比视频流协议更重要,你想要什么,什么可以满足你的需求,这个很大程度上是需求在前,选择使用什么视频流是比较靠后的. 目前Easy系列互联网直播服务将全线支持HLS.RTMP.HTP-FLV ...
- EasyNVR对接EasyCloud视频云平台进行云端录像
EasyCloud视频云平台是一套能够接入各种类型流,进行统一的设备管理.直播.录像.回放的视频平台,同时,EasyCloud视频云平台集成了云端运维功能,在云端就可以直接维护和控制各个现场的软件运行 ...
- jQuery插件——1.编写规则
jQuery插件编写规则如下: 1.命名规则:jquery.[插件名称].js 2.所有对象方法都应当附加到jQuery.fn对象上:所有的全局方法都应当附加到jQuery对象上. 3.在插件内部,t ...
- 【转】 通过VMLibrary在client partition上安装AIX全程实录
现场实录,应该对新手有所帮助,因为本人就是新手...最近一段时间跟Linux无缘,都是在和PowerVM打交道,博文更新慢,不赖我,哈哈本文参考链接:AIX Migration with File-B ...
- pc端监听屏幕实现导航固定定位
要点:占位符 js,监听屏幕滚动事件,当滚动条距离浏览器顶部的距离 大于 要固定定位开始以下元素的距离,则给要固定元素添加fixed样式. 初始化方法时,要给占位符添加样式 function sort ...
- java中参数传递的问题
public class Test { public static void main(String[] args) { StringBuffer a = new StringBuffer(" ...
- trait特性
1.trait特性可以和特化或者偏特化结合. 2.trait可以和类型转换结合.