/**
* 属性工具类
* @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. (转)linux进程 linux线程 信息查看 ps top pstree

    原文:https://blog.csdn.net/xiaoliuliu2050/article/details/81912202 https://blog.csdn.net/u011734144/ar ...

  2. 05-TypeScript中的方法新功能(下)

    再TypeScript中,方法还有一些新功能能够让我们更好的控制方法执行. 1.Generator方法: yield关键字用于控制方法在执行的时候暂停住,后续方法调用方又可以从暂停的地方继续执行,这种 ...

  3. 从零开始学 Web 之 jQuery(三)元素操作,链式编程,动画方法

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  4. C++中的字符串可以这样换行写

    运行结果:

  5. linux下xdebug的安装和配置方法

    xdebug简介 Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. xdebug安装 首先让php错误显示,只需要修改php.in ...

  6. 深度学习之PyTorch实战(3)——实战手写数字识别

    上一节,我们已经学会了基于PyTorch深度学习框架高效,快捷的搭建一个神经网络,并对模型进行训练和对参数进行优化的方法,接下来让我们牛刀小试,基于PyTorch框架使用神经网络来解决一个关于手写数字 ...

  7. Apollo 2 如何支持 @Value 注解自动更新

    前言 Apollo 在 v0.10.0 版本后,支持自动更新.v0.10.0之前的版本在配置变化后不会重新注入,需要重启才会更新. 也就是说,如果一个属性加入了 @Value 注解,并且这个配置在配置 ...

  8. LInux Crontab及命令

    定时任务(cron job)被用于安排那些需要被周期性执行的命令.利用它,你可以配置某些命令或者脚本,让它们在某个设定的时间内周期性地运行.cron 是 Linux 或者类 Unix 系统中最为实用的 ...

  9. iview 刷新滞后于html问题

    一.问题描述 每次刷新页面,下面的内容就会一闪而过. 一闪而过后恢复正常: 二.解决 问题代码: @*<span>修改密码</span>*@ @*<span>{{m ...

  10. php下载远程图片到本地

    在使用 PHP 做简单的爬虫的时候,我们经常会遇到需要下载远程图片的需求,所以下面来简单实现这个需求1:使用curl 比如我们有下面这两张图片: $images = [ 'https://img.al ...