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 ...
随机推荐
- 如何使用NSOperations和NSOperationQueues
原文地址: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues 本文由 大侠自来也(泰然教 ...
- Kotlin——初级篇(二):变量、常量、注释
在Kotlin中的变量.常量以及注释多多少少和Java语言是有着不同之处的.不管是变量.常量的定义方式,还是注释的使用.下面详细的介绍Kotlin中的变量.常量.注释的使用.以及和Java的对比. 如 ...
- 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法
原创文章,欢迎转载.转载请注明:关东升的博客 可以在原始类型上扩展计算属性,包括实例计算属性和静态计算属性.添加计算属性的定义,与普通的计算属性的定义是一样的. 实例计算属性示例:在网络编程时,为了减 ...
- 《从零开始学Swift》学习笔记(Day 11)——数据类型那些事儿?
原创文章,欢迎转载.转载请注明:关东升的博客 在我们学习语言时都会学到这种语言的数据类型,在Swift中数据类型有那些呢?整型.浮点型.布尔型.字符.字符串这些类型是一定有的,其中集合. ...
- A Simple Problem with Integers(线段树)
F - A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:% ...
- .NET架构师必备知识
.NET架构师,我归纳一下要学的知识: 成为优秀程序员,需要学好的知识: 1. 面向对象编程.UML画图.设计模式.代码重构 2. 常用ORM工具 3. MVC,WCF,XMl, JQuery ,S ...
- IlRuntime + protobuf-net
环境unity566,.net2.0 下载protobuf-net https://github.com/mgravell/protobuf-net/tree/r668 因为这个vs2015就可以打开 ...
- Dapper-Extensions
https://github.com/tmsmith/Dapper-Extensions
- C#对Excel中指定一列或一行实现隐藏或显示!
C#对Excel中指定一列或一行实现隐藏或显示!不会,求指导!
- JavaScript:基础扩展(1)——JSON
JavaScript:扩展知识(1)——JSON 理解: 关于 JSON,最重要的是要理解它是一种数据格式,不是一种编程语言.虽然具有相同的语法形式,但 JSON 并不从属于 JavaScript.而 ...