package com.ctcti.webcallcenter.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* 读取Properties综合类,默认绑定到classpath下的config.properties文件。
* @author
*/
public class PropertiesUtil {
//配置文件的路径
private String configPath=null;

/**
* 配置文件对象
*/
private Properties props=null;

/**
* 默认构造函数,用于sh运行,自动找到classpath下的config.properties。
*/
public PropertiesUtil() throws IOException{

//1方法

InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("config.properties");

//2.方法
// String path = CommonUtils.class.getClassLoader().getResource("config.properties").getPath();
// InputStream is = new FileInputStream(path);

//3方法通过文件加载
// String dirPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取config.properties文件所在的父目录
// File file = new File(dirPath,"config.properties");

props = new Properties();
props.load(in);
//关闭资源
in.close();
}

/**
* 根据key值读取配置的值
* Jun 26, 2010 9:15:43 PM
* @author 朱志杰
* @param key key值
* @return key 键对应的值
* @throws IOException
*/
public String readValue(String key) throws IOException {
return props.getProperty(key);
}

/**
* 读取properties的全部信息
* Jun 26, 2010 9:21:01 PM
* @author 朱志杰
* @throws FileNotFoundException 配置文件没有找到
* @throws IOException 关闭资源文件,或者加载配置文件错误
*
*/
public Map<String,String> readAllProperties() throws FileNotFoundException,IOException {
//保存所有的键值
Map<String,String> map=new HashMap<String,String>();
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
map.put(key, Property);
}
return map;
}

/**
* 设置某个key的值,并保存至文件。
* Jun 26, 2010 9:15:43 PM
* @author 朱志杰
* @param key key值
* @return key 键对应的值
* @throws IOException
*/
public void setValue(String key,String value) throws IOException {
Properties prop = new Properties();
InputStream fis = new FileInputStream(this.configPath);
// 从输入流中读取属性列表(键和元素对)
prop.load(fis);
// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(this.configPath);
prop.setProperty(key, value);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos,"last update");
//关闭文件
fis.close();
fos.close();
}
}

读取.properties配置信息的更多相关文章

  1. spring boot mybatis XML文件读取properties配置信息

    配置文件application.properties中相关配置信息可以在部署以后修改,引用配置信息可以在代码和mybatis的映射文件中 1.JAVA代码 可以通过变量去读取 application. ...

  2. java Properties 配置信息类

    Properties(配置信息类):主要用于生产配置文件和读取配置文件信息. ----> 是一个集合类 继承HashTable 存值是以键-值的方式. package com.beiwo.io; ...

  3. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  4. spring读取加密配置信息

    描述&背景Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存 ...

  5. java读取properties配置文件信息

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  6. Configutation读取properties文件信息

    commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件.XML文件.JNDI资源.来自JDBC Da ...

  7. spring boot --- 使用 注解 读取 properties 文件 信息

    1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...

  8. PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)

    http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)

  9. JDBC通过配置文件(properites)读取数据库配置信息

    扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...

随机推荐

  1. Deep Learning 30: 卷积理解

    一.深度卷积神经网络学习笔记(一): 1. 这篇文章以贾清扬的ppt说明了卷积的实质,更说明了卷积输出图像大小应该为: 假设输入图像尺寸为W,卷积核尺寸为F,步幅(stride)为S(卷积核移动的步幅 ...

  2. XMU 1056 瞌睡 vs 听课 【动态规划】

    1056: 瞌睡 vs 听课 Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 19  Solved: 6[Submit][Status][Web Boar ...

  3. [IMX6DL][Android4.4] 电池低电量告警提示【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/51789964 之前版本的电池电量低是通过发送 intent ACTION_BATTERY_L ...

  4. I.MX6 android mkuserimg.sh

    /************************************************************************** * I.MX6 android mkuserim ...

  5. gunicorn部署Flask服务

    作为一个Python选手,工作中需要的一些服务接口一般会用Flask来开发. Flask非常容易上手,它自带的app.run(host="0.0.0.0", port=7001)用 ...

  6. 使用Python操作Redis应用场景

    1. 安装pyredis 首先安装pip   1 2 3 4 5 6 7 8 <SHELL># apt-get install python-pip ...... <SHELL> ...

  7. 客户端与服务器持续同步解析(轮询,comet,WebSocket)

    在B/S模型的Web应用中,客户端常常需要保持和服务器的持续更新.这种对及时性要求比较高的应用比如:股票价格的查询,实时的商品价格,自动更新的twitter timeline以及基于浏览器的聊天系统( ...

  8. Ubuntu 12.04下安装配置体验GNOME 3(转载)

    转自:http://www.tuicool.com/articles/zIbeIj 自己并不是一个思想前卫的人,穿衣审美也都是大众眼光.但是唯独喜欢在计算机方便尝试最新,心肝情愿的做小白鼠.近日,按耐 ...

  9. bzoj 1068: [SCOI2007]压缩【区间dp】

    神区间dp 设f[l][r][0]为在l到r中压缩的第一个字符为M,并且区间内只有这一个M,f[l][r][0]为在l到r中压缩的第一个字符为M,并且区间内有两个及以上的M 然后显然的转移是f[i][ ...

  10. bzoj 1625: [Usaco2007 Dec]宝石手镯【背包】

    裸的01背包 #include<iostream> #include<cstdio> using namespace std; int c,n,w,v,f[20001]; in ...