读取.properties配置信息
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配置信息的更多相关文章
- spring boot mybatis XML文件读取properties配置信息
配置文件application.properties中相关配置信息可以在部署以后修改,引用配置信息可以在代码和mybatis的映射文件中 1.JAVA代码 可以通过变量去读取 application. ...
- java Properties 配置信息类
Properties(配置信息类):主要用于生产配置文件和读取配置文件信息. ----> 是一个集合类 继承HashTable 存值是以键-值的方式. package com.beiwo.io; ...
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- spring读取加密配置信息
描述&背景Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存 ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- Configutation读取properties文件信息
commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件.XML文件.JNDI资源.来自JDBC Da ...
- spring boot --- 使用 注解 读取 properties 文件 信息
1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...
- PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)
http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)
- JDBC通过配置文件(properites)读取数据库配置信息
扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...
随机推荐
- JavaScript对象(复习笔记)
js对象 对象构造器 function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname ...
- frameset 框架整体退出登录的问题
1 设置其他的页面都验证session,如果session不存在就跳转到 Login 页: 2 Login中添加下面的js代码: <script language="JavaScrip ...
- URAL2104. Game with a Strip(博弈)
There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2n, n squares ...
- bzoj 3872 [ Poi 2014 ] Ant colony —— 二分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 从食蚁兽所在的边向叶节点推,会得到一个渐渐放大的取值区间,在叶子节点上二分有几群蚂蚁符 ...
- 《StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation》论文笔记
---恢复内容开始--- Motivation 使用单组的生成器G和判别训练图片在多个不同的图片域中进行转换 效果确实很逆天,难怪连Good Fellow都亲手给本文点赞 Introduction 论 ...
- 09_传智播客iOS视频教程_自动释放池与NSLog函数
不要管什么是自动释放池,现在给你讲你也听不懂.就业班才讲,不要知道太多,知道太多对你不好.电影里面死的最惨的人就是知道最多的人.把代码写到自动释放池里面就可以了.NSLog是printf的增强版,它增 ...
- Struts Filter告警:FilterDispatcher <<< is deprecated! Please use the new filters!
在struts2.3.14下,web.xml中使用 <filter> <filter-name>struts2</filter-name> <!-- < ...
- TCP/IP协议的建立连接与关闭连接过程
一.建立连接(三次握手) 第一次握手:建立连接时,客户端发送SYN(seq=x)包到服务器,并进入SYN_SENT状态,等待服务器的确认.SYN:同步序列编号(Synchronize Sequence ...
- win10解决vc++6.0不兼容问题方法
这个方法我是可以用了 所以就写在着勒... 1 这个是百度云链接 先下载这个东西 放在电脑上 http://pan.baidu.com/s/1c2MihLA(一个MSDEV.EXE) 2然后找到这个目 ...
- 洛谷P2787 语文1(chin1)- 理理思维(珂朵莉树)
传送门 一看到区间推倒……推平操作就想到珂朵莉树 区间推平直接assign,查询暴力,排序的话开一个桶统计,然后一个字母一个字母加就好了 开桶统计的时候忘了保存原来的左指针然后挂了233 //mina ...