Java读取.properties配置文件
来源:互联网 发布:中国亚投行失败了知乎 编辑:程序博客网 时间:2024/06/05 07:06
未经允许不得转载,谢谢!
/*1.根据配置文件是否被更改进行读取.properties配置文件的工具类,初始情况下一次性读取存到map集合中,当文件被更改后对更改的文件的MD5值重新判断,如果不同则重新读取*/
代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* 单例模式创建RWProperties,用于读取Properties配置文件信息
*/
public class RWProperties{
//私有并静态化本类对象
private static RWProperties rWProperties = null;
//创建Map对象
private static Map<String, Map<String,String>> proMap = new HashMap<String, Map<String,String>>();
//用于存放文件的MD5值
private static Map<String, String> fileHashmap = new HashMap<String, String>();
//该map存储默认的配置文件
private static Map<String, String> defaultproMap = new HashMap<String, String>();
private Properties pro = null;
private InputStream is = null;
//本类构造函数
private RWProperties(){};
/**
* 拿到配置文件所在目录,遍历出其中所有的配置文件,存入到一个String[]集合中
* @return listFilePath
*/
public static String[] initmethod(String confPath){
//配置文件所在目录名
String taskconfPath = confPath;
//拿到目录所在路径
String listfilepath = RWProperties.class.getClassLoader().getResource(taskconfPath).getPath();
System.out.println(listfilepath);
//遍历该目录下所有的配置文件,存入到String[]数组中
File listfile = new File(listfilepath);
String[] listFilePath = listfile.list();
//如果listFilePath下边还有目录则循环遍历知道发现.properties配置文件
for(String l : listFilePath){
File innerfile = new File(l);
if(innerfile.isDirectory()){
initmethod(l);
}
}
return listFilePath;
}
/**|对外提供获取本类的一个入口
* @return
*/
public static RWProperties getInstance() {
if(rWProperties==null){
rWProperties = new RWProperties();
}
return rWProperties;
}
/**获取已经读取过配置文件所有属性的Map对象,当配置文件修改后,在读取的时候先比较配置文件的MD5值与静态map对象中已经存在的MD5值是否相等
* 如果相等,则说明文件没有被改动过,直接返回map集合对象
* 如果不相等,则说明文件被改动过,重新读取配置文件,返回新的map集合对象
* @return proMap:包含所有配置信息的Map集合,集合中的形式是: Map<String, Map<String,String>>
*/
public Map<String, Map<String,String>> getProMap() {
String[] listFilePath = initmethod("taskconfPath");
//System.out.println(listFilePath[0]);
try {
for(int i=0;i<listFilePath.length;i++){
Map<String, String> innerproMap = new HashMap<String, String>();
//拿到配置文件的路径
String filepath = RWProperties.class.getClassLoader().getResource("taskconfPath/"+listFilePath[i]).getPath();
//System.out.println(filepath);
//该配置文件的MD5标识
String sid = Integer.toString(i);
//得到该文件的MD5值
String fileMD5 = FileMd5.getFileMD5(filepath);
System.out.println("第"+i+"个配置文件生成的MD5值:"+fileMD5);
//获取fileHashmap中的对应文件的MD5值
String MD5 = fileHashmap.get("fileMD5"+sid);
System.out.println("先获取集合中存储的第"+i+"个配置文件的MD5值:"+MD5);
if(MD5==null){
is = new FileInputStream(filepath);
pro = new Properties();
pro.load(is);
//获得任务ID
String TASK_ID = pro.getProperty("TASK_ID");
Enumeration<String> e = (Enumeration<String>) pro.propertyNames();
while (e.hasMoreElements()) {
String key = e.nextElement().toString();
String value = pro.get(key).toString();
innerproMap.put(key, value);
}
System.out.println("第"+i+"次读信息到innerproMap中:"+innerproMap);
proMap.put(TASK_ID, innerproMap);
is.close();//关闭流
//把这个配置文件的MD5值写入map集合中
fileHashmap.put("fileMD5"+sid, fileMD5);
System.out.println("fileHashmap中存在的MD5值:"+fileHashmap);
}else {
if(!MD5.equals(fileMD5)){
System.out.println("第"+i+"个配置文件的MD5值不相等,要重新读配该置文件");
//把这个配置文件的MD5值写入map集合中
fileHashmap.put("fileMD5"+sid, fileMD5);
is = new FileInputStream(filepath);
pro = new Properties();
pro.load(is);
Enumeration<?> e = pro.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = (String) pro.get(key);
innerproMap.put(key, value);
}
System.out.println("修改后第"+i+"次读信息到innerproMap中:"+innerproMap);
//获得任务ID
String TASK_ID = pro.getProperty("TASK_ID");
proMap.put(TASK_ID, innerproMap);
is.close();//关闭流
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return proMap;
}
/**
* 获取默认的配置文件中的信息存入到defaultproMap集合中
* @return defaultproMap:包含所有默认的配置文件信息,格式是:Map<String,String>
*/
public Map<String,String> getDefaultProMap(){
try {
//拿到默认的配置文件的路径
String defaultpath = RWProperties.class.getClassLoader().getResource("default.properties").getPath();
//读入流
is = new FileInputStream(defaultpath);
pro.load(is);
//遍历出所有的配置信息,以键值对的形式存入到defaultproMap集合中
Enumeration<?> e = pro.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = (String) pro.get(key);
defaultproMap.put(key, value);
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return defaultproMap;
}
/**
* 根据传入的属性名称获取配置文件中相应的属性
* @param name:配置文件中的属性名
* @return property:属性名对应的属性值
*/
public String getAttr(String name){
pro = new Properties();
String[] listFilePath = initmethod("taskconfPath");
try {
for(int i=0;i<listFilePath.length;i++){
//拿到配置文件的路径
String filepath = RWProperties.class.getClassLoader().getResource("taskconfPath/"+listFilePath[i]).getPath();
is = new FileInputStream(filepath);
}
pro.load(is);
String property = pro.getProperty(name);
return property;
} catch (Exception e) {
}
return null;
}
/**
* 根据传入的属性名称设置配置文件中相应的属性,该方法达不到,在修改后,再次加载getProMap方法重新获取参数的map集合,不可用
* @param name
* @return
* @throws IOException
*/
public void setAttr(String name,String value) throws IOException{
pro = new Properties();
///保存属性到配置文件
try {
//String path = RWProperties.class.getClassLoader().getResource(fileName).getPath();
String[] listFilePath = initmethod("taskconfPath");
FileOutputStream out = new FileOutputStream("src/"+"taskconfPath/"+listFilePath[0]);
pro.setProperty(name, value);
pro.store(out,null);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 以List的形式向配置文件中写入,该方法达不到,在修改后,再次加载getProMap方法重新获取参数的map集合,不可用
* @param filename
* @param list
* @throws IOException
*/
public void set(List<String[]> list) throws IOException{
//String path = RWProperties.class.getClassLoader().getResource(fileName).getPath();//这种路径 src下的配置文件不能与classes下的同步,为什么?
String[] listFilePath = initmethod("taskconfPath");
FileOutputStream out = new FileOutputStream("src/"+"taskconfPath/"+listFilePath[0],true);
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i)[0] + list.get(i)[1]);
pro.setProperty(list.get(i)[0], list.get(i)[1]);
}
pro.store(out, null);
out.flush();
out.close();
}
public static void main(String[]args) throws IOException{
//测试初始化时,集合中的信息显示
RWProperties instance1 = RWProperties.getInstance();
Map<String, Map<String, String>> proMap1 = instance1.getProMap();
System.out.println(proMap1);
//测试修改第一个配置文件的结果
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RWProperties instance2 = RWProperties.getInstance();
Map<String, Map<String, String>> proMap2 = instance2.getProMap();
System.out.println(proMap2);
//测试修改第二个配置文件的结果
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RWProperties instance3 = RWProperties.getInstance();
Map<String, Map<String, String>> proMap3 = instance3.getProMap();
System.out.println(proMap3);
}
}
2. 得到文件的MD5值;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
public class FileMd5 {
/**
* 根据路径创建文件,得到该文件的MD5值
* @param filepath
* @return
*/
public static String getFileMD5(String filepath) {
File file = new File(filepath);
if (!file.exists() || !file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(32);
}
}
- java 读取properties配置文件
- Java读取properties配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java 读取*.properties配置文件
- Java读取properties配置文件
- java 读取配置文件(properties)
- java读取 .properties配置文件
- java读取properties配置文件
- Java读取properties配置文件
- Java读取properties配置文件
- java读取配置文件properties
- java读取properties配置文件
- java读取properties配置文件
- java读取properties配置文件
- java读取properties配置文件
- Java读取Properties配置文件
- java读取.properties配置文件
- 面向对象进阶篇之中介者模式
- 【读书笔记《Bootstrap 实战》】1.初识Bootstrap
- 快速认识Java内存区域划分
- phpstorm swoole和CI代码自动补全
- 第一个csdn博客
- Java读取.properties配置文件
- 字符串的格式化
- MFC编辑框实现回车换行
- ViewPager禁止预加载以及ViewPagerAdapter的处理
- PHP上传文件的安全问题
- ClassCastException: com.demoapp.dialog.img.LoadingView cannot be cast to com.demolib.dialog.img.Load
- 完成端口模型IOCP详解 (一)
- ECSHOP模板修改
- 20170619的代码