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. Cron Expression

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  2. VS2010调用外部webservice

    vs2010怎么调用web服务webservice方法,以vs2010为例.Vs的各个版本的此项功能操作基本一致. 工具/原料 vs2010 在“服务引用设置”对话框中,单击“添加 Web 引用”. ...

  3. Enum的基本使用

    package enum_test; public enum Shrubbery { GROUND, CRAWLING, HANGING } package enum_test; public cla ...

  4. AC自动机简明教程

    不会kmp和Trie树的请点击右上角X. AC自动机与kmp的唯一区别便是从单模式串变成了多模式串. 那么与kmp相同,AC自动机中的fail指针是指向当前状态的最长后缀. 当然这个后缀要在Trie树 ...

  5. 网络抓包工具wireshark and tcpdump 及其实现基于的libpcap

    最近无意中看到博客园中一篇介绍wireshark的文章,写得不错,它简单清楚介绍了wireshark的使用 简介 wireshark以前叫做Ethereal, 在大学时候的网络课程中就常看到它,它是世 ...

  6. FLASH OTP

    OTP 软件加密运用参考 为了防止软件被他人盗用,spansion flash给每个芯片植入了代表身份的unique ID,增加了OTP扇区,目前我们可以运用这两个特性,来实现软件的加密保护.软件加密 ...

  7. zoj3777(状态压缩)

    题目阐述: 给定n个座位,n个人,每个人可以做n个位置中的任意一个,P[i][j]代表第i个人做第j个位置获得的分数,求有多少种排列方式使得获得的分数大于等于M. 这道题跟数位dp的思想很像,都是穷举 ...

  8. bzoj4631

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 260  Solved: 133[Submit][Status][Discuss] ...

  9. 让padding不影响容器总长度

    增加CSS属性: box-sizing:border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box;

  10. J20170521-ts

    組み込み 内置 リダイレクト 重定向 ディスクリプタ 描述符 バッファリング n 缓冲