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工具类的更多相关文章

  1. 【转载】ASP.NET工具类:文件夹目录Directory操作工具类

    在ASP.NET开发网站的过程中,有时候会涉及到文件夹相关操作,如判断文件夹目录是否存在.删除文件夹目录.创建文件.删除文件.复制文件夹等等.这一批有关文件目录的操作可以通过Directory类.Fi ...

  2. 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等

    以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...

  3. 使用MAT(Memory Analyzer Tool)工具分析dump文件--转

    原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...

  4. MyBatis Java不同方式加载文件时的路径格式问题、Mybatis中加载.properties文件

    public class LoadPropTest { public static void main(String[] args) throws IOException { //一.Properti ...

  5. 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  6. Java程序读取Properties文件

    一.如果将properties文件保存在src目录下 1.读取文件代码如下: /** * 配置文件为settings.properties * YourClassName对应你类的名字 * / pri ...

  7. 用类加载器的5种方式读取.properties文件

    用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...

  8. 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类

    import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...

  9. Java读取properties文件工具类并解决控制台中文乱码

    1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...

随机推荐

  1. Android的那些轮子

    整个AOSP代码,包天包地,从kernel,libc,gui,net……简直包括了绝大多普通程序员职业生涯涉及的整个领域.其实,开源界早已经遇到并且解决了Android的很多问题,不过google讨厌 ...

  2. 【Xamarin挖墙脚系列:对设备/模拟器的查看调试监听】

    原文:[Xamarin挖墙脚系列:对设备/模拟器的查看调试监听] 有时候我们需要查看模拟器中的文件,比如进行了文件IO操作,sqlite数据库的操作等.我们想查看内容,这时候,如何将内容导出来?由于A ...

  3. android中handler中 obtainmessge与New message区别

    obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new new需要重新申请,效率低,obtianmessage可以循环利用: //use Handler.obtainMess ...

  4. maven 搭建企业级web项目

    就看这篇文章了:http://www.cnblogs.com/quanyongan/archive/2013/05/28/3103243.html

  5. 【转】eclipse内存设置,tomcat内存设置,查看内存大小

    原文网址:http://www.cnblogs.com/youngjoy/p/4239978.html 首先可以通过java/jdk/bin下的java visualVM查看eclipse的内存大小和 ...

  6. 判斷作業系統為 64bit 或 32bit z

    有時我們在開發Windows 桌面應用程式時,會發生一些弔詭的事情,作業系統位元數就是一個蠻重要的小細節,若您寫的應用程式在Windows 的32bit 作業系統上可以完美的運行,但不見得在64bit ...

  7. Linux 修改默认文件关联打开程序

    从总体上讲 /etc/gnome/defaults.list 保存了全局的打开方式-/.local/share/applications/mimeapps.list 保存了个人的打开方式当这两个文件不 ...

  8. 《C语言程序设计现代方法》第4章 表达式

    C语言的一个特点就是它更多地强调表达式而不是语句,表达式是表示如何计算值的公式. 当表达式包含两个或更多个相同优先级的运算符时,运算符的结合性(associativity)开始发挥作用.如果运算符是从 ...

  9. KTV点歌系统播放原理

    北大青鸟KTV点歌系统播放原理 指导老师--原玉明 1.首先知道有两个类(PlayList   Song) PlayList类中有一方法 public static bool AddSong(Song ...

  10. .NET Linq/MVC/架构

    学习参考: http://www.cnblogs.com/wangiqngpei557/p/3576685.html