PropertiesUtil 读取properties
package com.midea.clean.util; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class PropertiesUtil { private static Properties props;
private static final String propertieFile = "/returnCode.properties"; private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class); private PropertiesUtil(String fileName) { } static{
try {
props = new Properties();
InputStream fis = PropertiesUtil.class.getResourceAsStream(propertieFile);
props.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
} private static void readProperties(String fileName) {
try {
props = new Properties();
InputStream fis = PropertiesUtil.class.getResourceAsStream(fileName);
props.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 获取某个属性
* @throws UnsupportedEncodingException
*/
public static String getProperty(String key) {
if (props == null) {
readProperties(propertieFile);
}
try { LOGGER.info("获取属性 {}",key);
String tmp = new String(props.getProperty(key).getBytes("ISO8859-1"), "UTF8");
LOGGER.info("获取属性 {}:{}", key, tmp);
return tmp;
} catch (UnsupportedEncodingException e) {
LOGGER.info("编码格式不支持:{}",e.getMessage());
return "";
}
} /**
* 获取所有属性,返回一个map,不常用 可以试试props.putAll(t)
*/
public Map getAllProperty() {
if (props == null) {
readProperties(propertieFile);
} Map map = new HashMap();
Enumeration enu = props.propertyNames();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
String value = props.getProperty(key);
map.put(key, value);
}
return map;
} /**
* 在控制台上打印出所有属性,调试时用。
*/
public void printProperties() {
props.list(System.out);
} /*public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(PropertiesUtil.getProperty("loginUsername"));
}*/
}
PropertiesUtil 读取properties的更多相关文章
- 读取.properties配置文件
方法1 public class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...
- java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
- java中Properties类及读取properties中属性值
本文为博主原创,未经允许不得转载: 在项目的应用中,经常将一些配置放入properties文件中,在代码应用中读取properties文件,就需要专门的类Properties类,通过这个类可以进行读取 ...
- Configutation读取properties文件信息
commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件.XML文件.JNDI资源.来自JDBC Da ...
- 使用Properties类和ResourceBundle类读取properties文件
一.介绍: 项目中经常把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码. 这里讲两种方式读取properties文件的方法 ...
- spring 读取properties文件--通过注解方式
问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...
- SpringBoot11 读取properties文件、发送邮件
1 读取properties文件 1.1 ResourceBundle 帮助我们事先国际化 1.1.1 前提 properties文件的命名方式必须体现除语言和国别 例如:test_zh_CN.pro ...
随机推荐
- pytorch学习记录
1.pytorch中的torch.split只能将tensor分割为相等的几分,如果需要特定的需求将tensor分割开,可以用torch.index_select.使用的时候,先生成index索引,示 ...
- 百度基础架构组-实习生面试(2016.08 java后台开发)
一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...
- java类的设计原则
1.内聚性 类应该描述一个单一的实体,所有的类操作应该在逻辑上相互配合,支持一个连贯性的目标.例如:学生和教职工属于不同的实体,应该定义两个类. 2.一致性 要遵循一定的设计风格和命名习惯.给类.方法 ...
- FBI树(第一次做建树题)
试题来源 NOIP2004 普及组 问题描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它的结 ...
- 『计算机视觉』经典RCNN_其一:从RCNN到Faster-RCNN
RCNN介绍 目标检测-RCNN系列 一文读懂Faster RCNN 一.目标检测 1.两个任务 目标检测可以拆分成两个任务:识别和定位 图像识别(classification)输入:图片输出:物体的 ...
- Android(二)——frida安装教程
pc端下载:pip install frida 之后就是在手机端或者模拟器下载对应版本的server 在手机或者模拟器上查看cpu版本型号,就根据这个来下载server system/build.pr ...
- php 文件压缩
1.php文件压缩代码: $zip = new ZipArchive;if($zip->open('aaa.zip',ZipArchive::OVERWRITE)===TRUE){ //aaa. ...
- git 连接github的配置
这段时间要先在git上开发,上传代码到github上,所以首先需配置本地的git和github. 这几篇文章都不错,可以参考一下,大体的配置都很清楚. 1:https://blog.csdn.net/ ...
- cookie VS localstorage
http://jerryzou.com/posts/cookie-and-web-storage/ cookie: 1. 数据上限4KB左右 2. 一般由服务器生成,可设置失效时间.如果在浏览器端生成 ...
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'list' in 'c
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'list' in ' ...