/**
* 属性工具类
* @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. HTML页面自动跳转,windows操作

    1) html的实现 <head> <!-- 以下方式只是刷新不跳转到其他页面 --> <meta http-equiv="refresh" cont ...

  2. 采用完成端口(IOCP)实现高性能网络服务器(Windows c++版)

    前言 TCP\IP已成为业界通讯标准.现在越来越多的程序需要联网.网络系统分为服务端和客户端,也就是c\s模式(client \ server).client一般有一个或少数几个连接:server则需 ...

  3. zmq setsockopt()

    zmq.RCVTIMEO:在一个recv操作返回EAGAIN错误前的最大时间 设置socket的接收操作超时时间.如果属性值是0,zmq_recv(3)函数将会立刻返回,如果没有接收到任何消息,将会返 ...

  4. 记录线上与本地docker镜像一致,但Dockerfile却构建失败的问题

    背景 公司新开了某个项目,我在新的服务器部署了docker环境,本着ctrl+c 和ctrl+v的惯例,直接把以前的php环境的Dockerfile文件直接复制到新项目服务器那里,结果构建失败,失败的 ...

  5. linux搭建sftp服务器

    转自:http://blog.csdn.net/superswordsman/article/details/49331539 最近工作需要用到sftp服务器,被网上各种方法尤其是权限设置问题搞得晕头 ...

  6. 【翻译】WPF4.5新特性(MSDN的翻译读不太懂)

    我很在意WPF的发展,有人说微软不再维护WPF了,无所谓,随他去. MSDN上有简体版:http://msdn.microsoft.com/zh-cn/library/vstudio/bb613588 ...

  7. ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程

    1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id  id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...

  8. [JSOI2010] 连通数

    Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. Output 输出一行一个整数,表示该图 ...

  9. WEB页获取串口数据

    最近做一个B/S的项目,需要读取电子秤的值,之前一直没做过,也没有经验,于是在网上找到很多  大致分两种 使用ActiveX控件,JS调用MSCOMM32.dll的串口控件对串口进行控制 使用C#语言 ...

  10. SQL 数据库加字段声明

    ALTER TABLE dbo.C_TrainPlan ADD MailCost DATETIME EXECUTE sp_addextendedproperty N'MS_Description', ...