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 ...
随机推荐
- 解决ios8 webView加载的地图无法定位问题
本文转载至http://www.cocoachina.com/bbs/read.php?tid-237825.html 1.在文件info.pilist 中导入 NSLocationWhenI ...
- Android开发:《Gradle Recipes for Android》阅读笔记1.5
这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...
- 构造三层时报错“程序 “D:\MyTest\....”不包含适合于入口点的静态"Main"方法”
错误 1 程序“D:\MyTest\EBookShop\Model\obj\x86\Debug\Model.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目” ...
- Android打印日志管理
做项目的时候,免不了要打印许多日志,等项目上线了,想要去除日志是又找不到在哪里怎么办?我们可以建立一个日志打印的类来统一管理: public class LogUtil { public static ...
- EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)
问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...
- 基于EasyNVR摄像机无插件直播流媒体服务器实现类似于单点登录功能的免登录直播功能
提出问题 EasyNVR是一套摄像机无插件直播的流媒体服务器软件,他可以接入各种各样的摄像机,再经过转化统一输出无插件化直播的RTMP.HLS.HTTP-FLV流,同时,EasyNVR为了数据安全,提 ...
- 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 " 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...
- DropdownList异步刷新GridView数据
前台代码: <div style=" clear:both; width:800px; text-align:center; margin-left:auto; margin-righ ...
- js引入方式的弹框方法2
html代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv=& ...
- _ 下划线 Underscores __init__
Underscores in Python https://shahriar.svbtle.com/underscores-in-python Underscores in Python This p ...