配置文件读取工具类--PropertiesUtil
/**
* 属性工具类
* @author admin
* 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
*/
public class PropertiesUtil {
private static Properties pro=null;
private static Map<String,Object> map=new HashMap<String, Object>(); //静态块中加载
static {
//读取多个配置文件
init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
// init("fileName2");
} //读取配置文件操作
private static void init(String fileName) {
try {
pro = new Properties();
//文件方式
// pro.load(new FileInputStream(new File(fileName)));
//类加载器方式
pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName)); //可遍历properties的key和value
//方式一 key(string)集合
/* for (String key : pro.stringPropertyNames()) {
System.out.println(key + "=" + pro.getProperty(key));
//存入map中
map.put(key, pro.getProperty(key));//string
map.put(key, pro.get(key));//对象
}*/ //方式二 key(对象)集合
/* Set<Object> keys=pro.keySet();
for (Object key : keys) {
System.out.println(key + "=" + pro.get(key));
//存入map中
map.put(key.toString(), pro.get(key));
}*/ //方式三 键值对集合(全面)
Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
for (Map.Entry<Object, Object> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
//存入map中
map.put(entry.getKey().toString(), entry.getValue());
}
//或迭代器方式
Iterator<Entry<Object, Object>> it=entrySet.iterator();
while(it.hasNext()) {
Map.Entry<Object, Object> entry =it.next();
System.out.println(entry.getKey() + "=" + entry.getValue());
//存入map中
map.put(entry.getKey().toString(), entry.getValue());
} //方式三 Enumeration(传统接口)
/* Enumeration<?> e = pro.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
//存入map中
map.put(key, value);
}*/ //保存属性到b.properties文件
// FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
// pro.setProperty("phone", "10086");
// pro.store(oFile, "The New properties file");
// oFile.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 简单调用方式
*/
//方式1:静态方法调用
public static String getValue(String key) {
return pro.getProperty(key);
} //方式2:静态常量调用
public static final String FILE_NAME=pro.getProperty("fileName"); public static void main(String[] args) {
System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
}
}
/**
* 属性工具类
* @author admin
* 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
*/
public class PropertiesUtil {
private static Properties pro=null;
private static Map<String,Object> map=new HashMap<String, Object>(); //静态块中加载
static {
//读取多个配置文件
init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
// init("fileName2");
} //读取配置文件操作
private static void init(String fileName) {
try {
pro = new Properties();
//文件方式
// pro.load(new FileInputStream(new File(fileName)));
//类加载器方式
pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName)); //可遍历properties的key和value
//方式一 key(string)集合
/* for (String key : pro.stringPropertyNames()) {
System.out.println(key + "=" + pro.getProperty(key));
//存入map中
map.put(key, pro.getProperty(key));//string
map.put(key, pro.get(key));//对象
}*/ //方式二 key(对象)集合
/* Set<Object> keys=pro.keySet();
for (Object key : keys) {
System.out.println(key + "=" + pro.get(key));
//存入map中
map.put(key.toString(), pro.get(key));
}*/ //方式三 键值对集合(全面)
Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
for (Map.Entry<Object, Object> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
//存入map中
map.put(entry.getKey().toString(), entry.getValue());
}
//或迭代器方式
Iterator<Entry<Object, Object>> it=entrySet.iterator();
while(it.hasNext()) {
Map.Entry<Object, Object> entry =it.next();
System.out.println(entry.getKey() + "=" + entry.getValue());
//存入map中
map.put(entry.getKey().toString(), entry.getValue());
} //方式三 Enumeration(传统接口)
/* Enumeration<?> e = pro.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
//存入map中
map.put(key, value);
}*/ //保存属性到b.properties文件
// FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
// pro.setProperty("phone", "10086");
// pro.store(oFile, "The New properties file");
// oFile.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 简单调用方式
*/
//方式1:静态方法调用
public static String getValue(String key) {
return pro.getProperty(key);
} //方式2:静态常量调用
public static final String FILE_NAME=pro.getProperty("fileName"); public static void main(String[] args) {
System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
}
}
配置文件读取工具类--PropertiesUtil的更多相关文章
- Java读取Maven工程下的配置文件,工具类
Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...
- excel读取 工具类
package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...
- properties文件读取工具类
项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...
- 文件读取工具类读取properties文件
1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...
- Java-Properties文件读取工具类
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configurat ...
- 开发读取.properties 配置文件工具类PropertiesUtil
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...
- java读取properties的工具类PropertiesUtil
package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...
- 一个读取propeties配置文件的工具类,线程安全的
public class ConfigUtil { private static Map<String,Properties> map = new HashMap<String,Pr ...
- C#中读写Xml配置文件常用方法工具类
场景 有时需要使用配置文件保存一些配置的属性,使其在下次打开时设置仍然生效. 这里以对xml配置文件的读写为例. 1.读取XML配置文. 2.写入XML配置文件. 3.匹配 XPath 表达式的第一个 ...
随机推荐
- 基于Docker+Prometheus+Grafana监控SpringBoot健康信息
在微服务体系当中,监控是必不可少的.当系统环境超过指定的阀值以后,需要提醒指定的运维人员或开发人员进行有效的防范,从而降低系统宕机的风险.在CNCF云计算平台中,Prometheus+Grafana是 ...
- nginx ngx_http_sub_module使用
ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的‘iuwai’全部替换成‘aaaaa‘,这个模块已经内置在nginx中,但是默认未安装,需要 ...
- Linux基础命令 ls
目录 1. ls 列出目录的内容 -a --all: -A --almost-all: -b --escape: --block-size=SIZE: --color: --d --directory ...
- Eureka安全认证
Eureka 服务加入安全认证只需要在之前的服务中增加三处步骤即可: 1.在Eureka Server中加入spring-boot-starter-security依赖 <dependencie ...
- MySQL 5.7最新版本的2个bug
好久没写博客了,都长草了.新业务上了5.7没遇到什么问题,虽然没遇到什么问题,但不代表没有问题,我有个习惯就是没事就喜欢逛逛percona的Blog,于是看到目前最新GA版本5.7.17的2个bug, ...
- solr源码分析之数据导入DataImporter追溯。
若要搜索的信息都是被存储在数据库里面的,但是solr不能直接搜数据库,所以只有借助Solr组件将要搜索的信息在搜索服务器上进行索引,然后在客户端供客户使用. 1. SolrDispatchFilter ...
- Redis之集群环境搭建
前面文章介绍了Redis的主从复制,虽然该模式能够在一定程度上提高系统的稳定性,但是在数据访问量比较大的情况下,单个master应付起来还是比较吃力的,这时我们可以考虑将redis集群部署,本文就来重 ...
- C# DataGrid 用法---极速入门测试
目标: 新手编程,只求DataGrid能运行起来,更多功能留在后面探讨. 步骤: 1.新建WPF文档 插入DataGrid控件. <Window x:Class="OASevl.Mai ...
- Ado.net和EF的区别
ado.net EF作为微软的一个ORM框架,通过实体.关系型数据库表之间的映射,使开发人员可以通过操作表实体而间接的操作数据库,大大的提高了开发效率.这样一来,.net平台下,我们与底层数据库的交互 ...
- MVC架构介绍-事件机制
实例产品基于asp.net mvc 5.0框架,源码下载地址:http://www.jinhusns.com/Products/Download 在.net框架中,事件是将事件发送者(触发事件的对象) ...