PropertiesUtil

package com.mm.util.properties;

import java.io.Closeable;
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.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Properties工具类
 * 
 * @author mm
 * 
 */
public class PropertiesUtil {

	/**
	 * 从src目录下属性文件获取Properties对象
	 * 
	 * @param fileName
	 *            文件名
	 * @return Properties对象
	 */
	private static Properties getPropertiesFromSrc(String fileName) {
		InputStream is = null;
		Properties pro = new Properties();
		try {
			is = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					fileName);
			pro.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeStream(is);
		}
		return pro;
	}

	/**
	 * 从文件路径中获取Properties对象
	 * 
	 * @param filePath
	 *            文件路径
	 * @return Properties对象
	 */
	private static Properties getPropertiesFromFile(String filePath) {
		InputStream is = null;
		Properties pro = new Properties();
		File file = new File(filePath);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			is = new FileInputStream(file);
			pro.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeStream(is);
		}
		return pro;
	}

	/**
	 * 保存Properties对象到文件中
	 * 
	 * @param pro
	 *            Properties对象
	 * @param savePath
	 *            保存目录
	 * @param desc
	 *            修改描述信息
	 */
	private static void savePropertiesFile(Properties pro, String savePath,
			String desc) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(new File(savePath));
			pro.store(os, desc);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeStream(os);
		}
	}

	/**
	 * 将Map存入属性文件
	 * 
	 * @param mapData
	 *            map数据
	 * @param filePath
	 *            文件路径
	 */
	public static void saveMapToProperties(Map<String, String> mapData,
			String filePath) {
		Properties pro = new Properties();
		pro.putAll(mapData);
		savePropertiesFile(pro, filePath, "add " + mapData.keySet().toString()
				+ " to properties!");
	}

	/**
	 * 将Properties对象存入XML
	 * 
	 * @param pro
	 *            Properties
	 * @param savePath
	 *            保存路径
	 */
	public static void savePropertiesToXML(Properties pro, String savePath) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(new File(savePath));
			pro.storeToXML(os, "write properties to xml");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeStream(os);
		}
	}

	/**
	 * 将属性文件转存为XML
	 * 
	 * @param proPath
	 *            属性文件路径
	 * @param savePath
	 *            XML路径
	 */
	public static void savePropertiesToXML(String proPath, String savePath) {
		OutputStream os = null;
		Properties pro = getPropertiesFromFile(proPath);
		try {
			os = new FileOutputStream(new File(savePath));
			pro.storeToXML(os, "write properties to xml");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeStream(os);
		}
	}

	/**
	 * 向属性文件中添加一条数据
	 * 
	 * @param filePath
	 *            文件路径
	 * @param key
	 *            键值
	 * @param value
	 *            属性值
	 */
	public static void addProperty(String filePath, String key, String value) {
		Properties pro = getPropertiesFromFile(filePath);
		pro.setProperty(key, value);
		savePropertiesFile(pro, filePath, "add " + key);
	}

	/**
	 * 将属性文件存入Map
	 * 
	 * @param filePath
	 *            文件路径
	 * @return Map
	 */
	public static Map<String, String> loadFilePropToMap(String filePath) {
		return properties2Map(getPropertiesFromFile(filePath));
	}

	/**
	 * 将src下属性文件存入Map
	 * 
	 * @param fileName
	 *            文件名
	 * @return Map
	 */
	public static Map<String, String> loadSrcPropToMap(String fileName) {
		return properties2Map(getPropertiesFromSrc(fileName));
	}

	/**
	 * 从src目录下根据键值从属性文件获取属性值,若不存在则返回null
	 * 
	 * @param fileName
	 *            文件名
	 * @param key
	 *            键值
	 * @return 属性值
	 */
	public static String getValueFromSrc(String fileName, String key) {
		return getPropertiesFromSrc(fileName).getProperty(key);
	}

	/**
	 * 从属性文件中获取属性值,若不存在则返回null
	 * 
	 * @param filePath
	 *            文件路径
	 * @param key
	 *            键值
	 * @return 属性值
	 */
	public static String getValueFromFile(String filePath, String key) {
		return getPropertiesFromFile(filePath).getProperty(key);
	}

	/**
	 * 根据键值删除属性文件中数据
	 * 
	 * @param filePath
	 *            文件路径
	 * @param key
	 *            键值
	 */
	public static void delPropertyByKey(String filePath, String key) {
		Properties pro = getPropertiesFromFile(filePath);
		Map<String, String> proData = new HashMap<String, String>();
		if (pro.containsKey(key)) {
			for (String str : pro.stringPropertyNames()) {
				proData.put(str, pro.getProperty(str));
			}
			proData.remove(key);
			pro.clear();
			pro.putAll(proData);
			savePropertiesFile(pro, filePath, "delete " + key);
		}
	}

	/**
	 * 清空Properties文件
	 * 
	 * @param filePath
	 *            文件路径
	 */
	public static void cleanPropertiesFile(String filePath) {
		Properties pro = getPropertiesFromFile(filePath);
		pro.clear();
		savePropertiesFile(pro, filePath, "clean properties file!");
	}

	/**
	 * 讲Properties对象中数据存入Map
	 * 
	 * @param pro
	 *            Properties
	 * @return Map
	 */
	private static Map<String, String> properties2Map(Properties pro) {
		Map<String, String> mapData = new HashMap<String, String>();
		Set<String> keys = pro.stringPropertyNames();
		for (String key : keys) {
			mapData.put(key, pro.getProperty(key));
		}
		return mapData;
	}

	/**
	 * 关闭流
	 * 
	 * @param closeables
	 */
	private static void closeStream(Closeable... closeables) {
		for (Closeable closeable : closeables) {
			try {
				if (closeable != null) {
					closeable.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值