PropertiesUtil

来源:互联网 发布:linux计数统计文件命令 编辑:程序博客网 时间:2024/06/07 23:24
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();}}}}

0 0
原创粉丝点击