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 ...
随机推荐
- Android无线测试之—UiAutomator UiSelector API介绍之八
对象搜索—特殊属性.节点与资源ID 一.特殊属性定位对象相关API 返回值 API 描述 UiSelector checkableboolean val) 是否可选择,一般开关组件上具有checkab ...
- Android开发:《Gradle Recipes for Android》阅读笔记1.7——仓库配置
repositories块告诉gradle哪里去寻找依赖,默认的android studio使用jcenter或者mavenCentral.jcenter仓库位于https://jcenter.bin ...
- Swift学习笔记(一):No such module 'Cocoa'
在xcode中创建一个Playground文件, 进行导包操作 ,import Cocoa 却提示No such module 'Cocoa' 原因是该Playground文件的platform 设置 ...
- Kotlin——初级篇(二):变量、常量、注释
在Kotlin中的变量.常量以及注释多多少少和Java语言是有着不同之处的.不管是变量.常量的定义方式,还是注释的使用.下面详细的介绍Kotlin中的变量.常量.注释的使用.以及和Java的对比. 如 ...
- F - 简单计算器(栈)
F - 简单计算器 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- influxDB概念
一.基本概念 1)database--数据库,这个同传统数据库的数据库概念. 2)measurement--数据表,在InfluxDB中,measurement即为表的作用,同传统数据库中的table ...
- Python 学习之旅
流程图 第一章 Python简介 第二章 Python基础 第三章 流程控制 第四章 字符编码 第五章 文件处理 第六章 函数 第七章 模块与包 第八章 面向对象 第九章 异常处理 第十章 ...
- VM和Windows Ping不通
连接模式:桥接 Linux上1.修改 /etc/sysconfig/network-scripts/ifcfg-enp0s3 文件 ONBOOT=yes2.service network restar ...
- Junit单元测试注入spring中的bean(转载)
转载自:http://blog.csdn.net/cy104204/article/details/51076678 一般对于有bean注入的类进行方法单元测试时,会发现bean对象并没有注入进来,对 ...
- Ubuntu 下 java 版本的切换
切换的方法很简单,使用下面的两个命令即可: update-alternatives --config java update-alternatives --config javac eg: root@ ...