properties配置文件的基本操作
对properties的基本操作
public class PropertiesUtil {
// 是否是文件
public static boolean isFile = false;
// 路径
public static String path;
// 单列properties
private static Properties properties = null;
// 构造方法初始化文件
public PropertiesUtil(String path) {
this.path = path;
File file = new File(path);
isFile = file.isFile();
// TODO Auto-generated constructor stub
}
public boolean isFile(String path){
return isFile;
};
// 把配置文件转化为对象
public Object propertiesToObject(Object object,String path) throws Exception, NoSuchMethodException{
if(!isFile(path)){
return null;
}
Field[] files = object.getClass().getDeclaredFields();
Properties properties = load(path);
for(Field field:files){
String fieldName = field.getName();
Class type = field.getType();
String methodFieldName = "set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
Method method = object.getClass().getMethod(methodFieldName, type);
method.invoke(object, properties.get(fieldName));
}
return object;
}
// 获取配置文件中所有的键值
public List<Object> getListKey(String path) throws IOException{
if(!isFile(path)){
return null;
}
Properties properties = load(path);
Set<Object> set = properties.keySet();
List<Object>list = new ArrayList<Object>(set);
return list;
}
// 获取配置文件中所有的值
public List<Object> getListValue(String path) throws IOException{
if(!isFile(path)){
return null;
}
Properties properties = load(path);
List<Object> list = new ArrayList<Object>();
for(Object key:properties.keySet()){
list.add(properties.get(key));
}
return list;
}
// 配置文件转成map集合
public Map<String,Object> getMapKeyValue(String path) throws IOException{
if(!isFile(path)){
return null;
}
Map<String,Object> resultMap = new HashMap<String,Object>();
Properties properties = load(path);
for(Object key : getListKey(path)){
resultMap.put((String)key, properties.get(key));
}
return resultMap;
}
public Properties load(String path) throws IOException{
if(properties == null){
InputStream stream = new FileInputStream(path);
properties = new Properties();
properties.load(stream);
}
return properties;
}
public static void main(String[]args) throws NoSuchMethodException, Exception{
String path = "D:/ceshiproperties/admessage.properties";
PropertiesUtil util = new PropertiesUtil(path);
System.out.println("此文件是否是一个文件"+util.isFile(path));
List<Object> listKey = util.getListKey(path);
for(Object object:listKey){
System.out.println("配置文件中所有的key值"+object.toString());
}
List<Object> listValue=util.getListValue(path);
for(Object object:listValue){
System.out.println("配置文件中所有的value"+object.toString());
}
AdMessage adMessage = new AdMessage();
util.propertiesToObject(adMessage, path);
System.out.println("值1"+adMessage.getMessage_content_changestatus_());
System.out.println("值2"+adMessage.getMessage_content_delete_());
System.out.println(util.getMapKeyValue(path));
System.exit(0);
}
}
对象bean的构造
public class AdMessage {
private String message_subject_of_delete_;
private String message_subject_changestatus_;
private String message_content_delete_;
private String message_content_changestatus_;
public String getMessage_subject_of_delete_() {
return message_subject_of_delete_;
}
public void setMessage_subject_of_delete_(String message_subject_of_delete_) {
this.message_subject_of_delete_ = message_subject_of_delete_;
}
public String getMessage_subject_changestatus_() {
return message_subject_changestatus_;
}
public void setMessage_subject_changestatus_(
String message_subject_changestatus_) {
this.message_subject_changestatus_ = message_subject_changestatus_;
}
public String getMessage_content_delete_() {
return message_content_delete_;
}
public void setMessage_content_delete_(String message_content_delete_) {
this.message_content_delete_ = message_content_delete_;
}
public String getMessage_content_changestatus_() {
return message_content_changestatus_;
}
public void setMessage_content_changestatus_(
String message_content_changestatus_) {
this.message_content_changestatus_ = message_content_changestatus_;
}
}
配置文件properties
路径:D:/ceshiproperties/admessage.properties
message_subject_of_delete_=liubing
message_subject_changestatus_=\u60A8\u6709\u4E00\u6761{0}\u6570\u636E\u88AB\u6FC0\u6D3B
message_content_delete_=15
message_content_changestatus_={0}\u6FC0\u6D3B\u4E86\u4E00\u6761{1}\u6570\u636E
properties配置文件的基本操作的更多相关文章
- 读取.properties配置文件
方法1 public class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- properties 配置文件中值换行的问题
在使用properties配置文件的时候我们经常碰到如下两个问题 1:当a=b中的b值内容特别长的时候为了阅读方便我们手动换行,但如果我们直接回车那么后面的数据就会丢失.那如何解决呢? 例如: a=a ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- java读取properties配置文件方法(一)
为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- jdbc基础 (二) 通过properties配置文件连接数据库
csdn博文地址:jdbc基础 (二) 通过properties配置文件连接数据库 上一篇描述了对mysql数据库的简单操作,下面来看一下开发中应该如何灵活应用. 因为jdbc对数据库的驱动加载.连接 ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
- Java读取Properties配置文件
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集.不过Properties的键和值都是字符串 ...
随机推荐
- spring中@注解的相关解释
@Component:@Controller:@Service:@Repository 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的 ...
- Groovy学习:第三章 Groovy开发环境
本章将继续深入Groovy语言,首先学习Groovy脚本,包括从命令行编译和运行Groovy脚本,Groovy Shell,和Groovy Console.你将学会使用Groovy语言来建立域对象.控 ...
- Aspose.Words转换为PDF的时候字体丢失的问题解决
系统中明明有字体的,Word中显示也正常,就是转换为PDF以后不正常,字体丢失,被替换成了等线字体 好一番研究,终于找到原因 ,原因是Windows\Fonts下的文件,有些只是虚拟的路径,真正的字体 ...
- zookeeper常用配置详解
#ZK中的一个时间单元.ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的.例如,session的最小超时时间是2*tickTime tickTime=2000 #Follower在启动过程中, ...
- JDBC getConnection细节
https://blog.csdn.net/luanlouis/article/details/29850811 概述 一般情况下,在应用程序中进行数据库连接,调用JDBC接口 ...
- ORA-01000 error
ORA-01000是最大开放游标错误,是Oracle数据库开发中极为常见的错误. 在Java的上下文中,当应用程序尝试打开更多ResultSet而不是数据库实例上的已配置游标时,会发生这种情况. 解决 ...
- VS2017编译64位CloudCompare
需求:编译一个支持读写las点云的CC,然后再开发CC插件实现业务功能. 编译环境: 1.Windows 10 2.Visual Studio 2017 Community 3.Qt 5.9.4 开源 ...
- C/C++ volatile
{ volatile和const关键很相似,都是修饰变量的,只是二者功能不一样. volatile在多线程当中经常使用,因为在某一线程多次调用某一个变量,编译器会进行优化,将该变量存放在在寄存器当中, ...
- @staticmethod和@classmethod区别
转载自: https://www.cnblogs.com/wyongbo/p/python_static_method.html https://www.cnblogs.com/champaign/p ...
- asp.net MVC遇到的问题
参考:http://blog.csdn.net/chenqiangdage/article/details/48996101 asp.netMVC 如何解决“上下文模型已在数据库创建后发生更改” 问题 ...