读取.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( ...
随机推荐
- 笔试题:求第M个到第N个素数之间全部素数
题目描写叙述 令Pi表示第i个素数. 现任给两个正整数M <= N <= 10000,请输出PM到PN的全部素数. 输入描写叙述: 输入在一行中给出M和N,其间以空格分隔. 输出描写叙述: ...
- mysql14---手动备份
PHP定时完成数据库的备份 1.手动备份数据库(表的)方法 cmd控制台(windows指令): mysqldump –u root –proot 数据库 [表名1 表名2..] > 文件路径 ...
- spring 简述
Spring的发展 1.1. Spring1.x 时代 在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和x ...
- casperjs userAgent的一些问题
casperjs 的options内的userAgent若设置为非正常浏览器的字符串,可能导致form无法正确提交. 表现为,this.click()失效,或evaluate(function(){$ ...
- bzoj 1086 王室联邦 —— 思路题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1086 一眼看去很是不会,于是看看TJ... https://blog.csdn.net/ly ...
- IJ:IJ来了
ylbtech-IJ:IJ来了 1.返回顶部 1. 2. 2.2. 3. 4. 2.返回顶部 1. 2. 3. 3. Welcome to IntelliJ IDEA返回顶部 1. 2.2. 2.2. ...
- linux 软件开发的源
deb http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiversedeb http://mirrors. ...
- E20180219-hm-xa
comparison n. 比较,对照; [语] 比喻; 比较级; conjunction n. 连接; 连词; 联合,结合物; (恒星.行星等的) 合; [例句] assignment n. 分 ...
- Python机器学习算法 — 朴素贝叶斯算法(Naive Bayes)
朴素贝叶斯算法 -- 简介 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法.最为广泛的两种分类模型是决策树模型(Decision Tree Model)和朴素贝叶斯模型(Naive Baye ...
- 洛谷 P3952 时间复杂度【模拟】
把No写成NO,WA了一发-- 现在看这题也不难-- 用一个栈,记一下前面F的字母,是否合法,合法的有多长,每次入栈弹栈即可 #include<iostream> #include< ...