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 ...
随机推荐
- const char*, char const*, char*const的区别
http://www.cnblogs.com/aduck/articles/2244884.html
- [转贴]一个将表格变成 INSERT 的SQL 语句的存储过程(sql server)
来源自http://vyaskn.tripod.com/code.htm#inserts SET NOCOUNT ON GO PRINT 'Using Master database' USE mas ...
- linux操作Oracle导入导出dmp数据命令
--清空该表数据 :非索引清空TRUNCATE TABLE GE_INTERFACE_MESSAGE; --数据库导出 指定导出某个用户的所有数据 包括表 索引 序列 存储过程 函数 等exp CX ...
- 【Android 开源】:最火的Android开源项目 第02期
21. drag-sort-listview DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能.重写了TouchIntercep ...
- Android4.0 -- UI控件之 Menu 菜单的的使用(一)
这一讲开始我们来讲一下Android中菜单的使用方法,菜单是应用中的普通的组件,主要是提供友好和专注的用户体验,你可以在你的Activity中使用 Menu APIs 来提供用户动作和其他选项的操 ...
- Android 解决安装Egit时Egit Mylyn和org.eclipse.team.core报错
为了让Aptana支持GitHub,需要安装Egit,但在的时候碰到两个错误,一个是关于缺少EGit Mylyn另一个是缺少org.eclipse.egit.import.feature.group. ...
- Oracle由ID生成父ID的函数
/*表结构*/ CREATE TABLE ly_md ( bh BYTE), mc BYTE), pym BYTE), f_bh BYTE), ch NUMBER, ID NUMBER ); INSE ...
- Java for selenium(webdriver) 环境搭建
开发环境 1. jdk1.7 2. Eclipse 3. selenium(selenium-java-2.42.2.zip) 将下载下来的 selenium-java-2.42.2.zip 解压, ...
- 神经网络中误差反向传播(back propagation)算法的工作原理
注意:版权所有,转载需注明出处. 神经网络,从大学时候就知道,后面上课的时候老师也讲过,但是感觉从来没有真正掌握,总是似是而非,比较模糊,好像懂,其实并不懂. 在开始推导之前,需要先做一些准备工作,推 ...
- Tornado源码探寻(准备阶段)
上一篇从一个简单的例子大致了解到Tornado框架的一个概述,同时也看清了web框架的本质. 接下来,我们从tornado程序的起始来分析其源码: 一.概述 上图是摘自朋友的博客里的内容,这张图很明确 ...