配置文件读取工具类--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 表达式的第一个 ...
随机推荐
- HoloLens开发手记 - 入门学习阶段总结
伴随着数月的期待,终于拿到了预订的HoloLens开发者版本套件.随着VR/AR/MR技术的热潮,国内外均对它们的应用与盈利前景持有积极的预期,这也直接导致了国内外当前投资VR/AR/MR技术的热潮. ...
- 【BJOI2019】光线 模拟
题目大意:有一束光线要依次穿过$n$块玻璃. 第i块玻璃的透射率为$a_i$,反射率为$b_i$. 问你有多少光能最终穿过所有玻璃. 数据范围:$n≤5\times 10^5$,答案对$9982443 ...
- iOS之Settings.Bundle的应用
Settings.Bundle Settings.Bundle支持六种配置项分别是:Title,MultiValue,Group,Slider,ToggleSwitch,TextField . Tit ...
- Spark之submit任务时的Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient memory
Spark submit任务到Spark集群时,会出现如下异常: Exception 1:Initial job has not accepted any resources; check your ...
- Spring Boot - 修改Tomcat默认的8080端口
前言 默认情况下,Spring Boot内置的Tomcat服务会使用8080端口启动,我们可以使用以下任何技巧去更改默认的Tomcat端口: 注:我们可以通过server.port=0配置,去自动配置 ...
- 从零开始学 Web 之 JavaScript(一)JavaScript概述
大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十六):容器部署项目
容器部署项目 这一章我们引入docker,采用docker容器的方式部署我们的项目. 首先需要有一个linux环境,并且安装 java 和 maven 以及 docker 环境,这个教程多如牛毛,不再 ...
- java ee期末项目相关
1.项目简介 本项目是对纸杯生产进行管理的的一个系统,从前端接收到订单,然后根据订单内容进行纸杯的生产.如下为该系统的总流程图: 1.项目系统架构图 3.系统用例图 4.ER图 主要的代码和相关文件见 ...
- Java并发编程笔记之基础总结(二)
一.线程中断 Java 中线程中断是一种线程间协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是需要被中断的线程根据中断状态自行处理. 1.void interrupt() 方法:中断线 ...
- 协程及Python中的协程
1 协程 1.1协程的概念 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.(其实并没有说明白~) 我觉得单说协程,比较抽象,如果对线程有一定了解 ...