/**
* 属性工具类
* @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的更多相关文章

  1. Java读取Maven工程下的配置文件,工具类

    Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...

  2. excel读取 工具类

    package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...

  3. properties文件读取工具类

    项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...

  4. 文件读取工具类读取properties文件

    1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...

  5. Java-Properties文件读取工具类

    import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configurat ...

  6. 开发读取.properties 配置文件工具类PropertiesUtil

    import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...

  7. java读取properties的工具类PropertiesUtil

    package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...

  8. 一个读取propeties配置文件的工具类,线程安全的

    public class ConfigUtil { private static Map<String,Properties> map = new HashMap<String,Pr ...

  9. C#中读写Xml配置文件常用方法工具类

    场景 有时需要使用配置文件保存一些配置的属性,使其在下次打开时设置仍然生效. 这里以对xml配置文件的读写为例. 1.读取XML配置文. 2.写入XML配置文件. 3.匹配 XPath 表达式的第一个 ...

随机推荐

  1. Autowired byType 与 byName 策略

    @Autowired是spring的注解,默认使用的是byType的方式向Bean里面注入相应的Bean.例如: @Autowiredprivate UserService userService;这 ...

  2. app测试自动化之定位元素

    app中元素定位是通过uiautomatorviewer来查看,这个是android sdk中自带的一个工具,可以在sdk家目录的tools下找到: 双击打开之后,点击第二个按钮即可把手机当前界面的元 ...

  3. web自动化测试---测试中其他一些常用操作

    一些其他常用操作如下: 1.最大化浏览器窗口 driver.maximize_window() 2.后退 driver.back() 3.前进 driver.forward() 4.刷新操作 driv ...

  4. Studying

    美团spark实践:http://tech.meituan.com/spark-in-meituan.html CDH5.6.0-HBase1.0.0:http://archive.cloudera. ...

  5. [java初探09]__关于java的包装类

    前言 在Java语言的学习过程中,我们逐渐的理解了Java面向对象的思想,与类和对象的应用.但是在基本数据类型的使用上,我们无法将其定义为一个对象,通过使用对象的方法来使用它们,但是Java语言的思想 ...

  6. TCP/IP 笔记 - 广播和本地组播

    在之前第二章介绍IP寻址的时候有介绍到,IP地址有4种:单播.组播.广播.任播. 单播,客户端与服务器之间点到点连接通信: 组播,在发送者和多个接收者(如某个特定的分组)之间实现点对多点的连接通信: ...

  7. Zabbix4.2.0使用Python连接企业微信报警

    目录 1. 配置企业微信 2. 脚本配置 2.1 安装python依赖的库 2.2 编写脚本 2. 搭建FTP 3. 配置Zabbix监控FTP 3.1 添加FTP模板 3.2 添加报警媒介 3.3 ...

  8. vue 周期函数

    先简单说说vue中周期函数 beforeCreate(创建前) created(创建后) beforeMount(载入前) mounted(载入后) beforeUpdate(更新前) updated ...

  9. 在JS中统计函数执行次数与执行时间

    假如想统计JS中的函数执行次数最多的是哪个,执行时间最长的是哪个,该怎么做呢? 1. 统计函数执行次数 2. 统计函数执行时间 3. 如何控制函数的调用次数 4. 如何控制函数的执行时间 一.统计函数 ...

  10. 深入理解JavaScript的事件循环(Event Loop)

    一.什么是事件循环 JS的代码执行是基于一种事件循环的机制,之所以称作事件循环,MDN给出的解释为 因为它经常被用于类似如下的方式来实现 while (queue.waitForMessage()) ...