Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年8月9日 11:31:12 星期二
http://fanshuyao.iteye.com/
package com.chinagas.common.utils; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class PropertiesUtils { private static Logger log = LoggerFactory.getLogger(PropertiesUtils.class); /**
* 根据文件名获取Properties对象
* @param fileName
* @return
*/
public static Properties read(String fileName){
InputStream in = null;
try{
Properties prop = new Properties();
//InputStream in = Object.class.getResourceAsStream("/"+fileName);
in = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
prop.load(in);
return prop;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} /**
* 根据文件名和键名获取值
* @param fileName
* @param key
* @return
*/
public static String readKeyValue(String fileName, String key){
Properties prop = read(fileName);
if(prop != null){
return prop.getProperty(key);
}
return null;
} /**
* 根据键名获取值
* @param prop
* @param key
* @return
*/
public static String readKeyValue(Properties prop, String key){
if(prop != null){
return prop.getProperty(key);
}
return null;
} /**
* 写入
* @param fileName
* @param key
* @param value
*/
public static void writeValueByKey(String fileName, String key, String value){
Map<String, String> properties = new HashMap<String, String>();
properties.put(key, value);
writeValues(fileName, properties);
} /**
* 写入
* @param fileName
* @param properties
*/
public static void writeValues(String fileName, Map<String, String> properties){
InputStream in = null;
OutputStream out = null;
try {
in = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(in);
String path = PropertiesUtils.class.getResource("/"+fileName).getPath();
out = new FileOutputStream(path);
if(properties != null){
Set<String> set = properties.keySet();
for (String string : set) {
prop.setProperty(string, properties.get(string));
log.info("更新"+fileName+"的键("+string+")值为:"+properties.get(string));
}
}
prop.store(out, "update properties");
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.flush();
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
} public static void main(String[] args) throws Exception {
//System.out.println("read="+read("config.properties")); //System.out.println("readKeyValue="+readKeyValue("config.properties","superAdmin")); //writeValueByKey(CC.WEIXI_PROPERTIES, "access_token", "ddd"); Map<String, String> properties = new HashMap<String, String>();
properties.put("access_token", "ddd2");
properties.put("access_token1", "ee2");
properties.put("bbbb", "bbbb");
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年8月9日 11:31:12 星期二
http://fanshuyao.iteye.com/
Property工具类,Properties文件工具类,PropertiesUtils工具类的更多相关文章
- 【转载】ASP.NET工具类:文件夹目录Directory操作工具类
在ASP.NET开发网站的过程中,有时候会涉及到文件夹相关操作,如判断文件夹目录是否存在.删除文件夹目录.创建文件.删除文件.复制文件夹等等.这一批有关文件目录的操作可以通过Directory类.Fi ...
- 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等
以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...
- 使用MAT(Memory Analyzer Tool)工具分析dump文件--转
原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...
- MyBatis Java不同方式加载文件时的路径格式问题、Mybatis中加载.properties文件
public class LoadPropTest { public static void main(String[] args) throws IOException { //一.Properti ...
- 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Java程序读取Properties文件
一.如果将properties文件保存在src目录下 1.读取文件代码如下: /** * 配置文件为settings.properties * YourClassName对应你类的名字 * / pri ...
- 用类加载器的5种方式读取.properties文件
用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...
- 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...
- Java读取properties文件工具类并解决控制台中文乱码
1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...
随机推荐
- Migrating from IntelliJ Projects
We might provide an automatic migration option in Android Studio in the future. For now, to migrat ...
- [笔记] 走进 Pocket,看看只有 20 位员工的 Pocket 是如何搞定 2000 万用户的
走进 Pocket,看看只有 20 位员工的 Pocket 是如何搞定 2000 万用户的 保持专注. 不断更新优先级. 对产品有主人翁意识. (觉得做好产品是自己的职责, 不是应付工作) 你的用户如 ...
- 14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构
14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是 B-trees Index r ...
- 【HDOJ】1239 Calling Extraterrestrial Intelligence Again
这题wa了很多词,题目本身很简单,把a/b搞反了,半天才检查出来. #include <stdio.h> #include <string.h> #include <ma ...
- Oracle - Unprocessed Material
create table kol.MTL_MATERIAL_TRANS_TMP_140325 as select * from MTL_MATERIAL_TRANSACTIONS_TEMP ; upd ...
- vi find和grep
linux grep和find命令 linux中强大且常用命令:find.grep 源码搜索:find . -name "*.xml" | xargs grep -Hna &quo ...
- JS链接页面
window.location.href = "index.php?op=xx&extend_op=xxc" 这样是在本页打开链接 window.open("ht ...
- HDU 4722 Good Numbers
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 Good Numbers Time Limit: 2000/1000 MS (Java/Othe ...
- Get familiar with key Frameworks of ios
Frameworks make your life easier as an iOS Developer. They allow you to reuse code written by other ...
- What does the “__block” keyword mean?
It tells the compiler that any variable marked by it must be treated in a special way when it is use ...