标签:
一、琅序
java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。
------------------------------------------------------
Java.util 中,Properties类继承自
Hashtable
1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到
key 所对应的 value。
2. load ( InputStream inStream)
,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对,以供 getProperty (
String key) 搜索。
3. setProperty ( String key, String value) ,调用 Hashtable
的方法 put方法。它通过调用基类的put方法来设置键 - 值对。
4. store ( OutputStream out, String
comments) ,以适合使用 load 方法加载到 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 -
值对写入到指定的文件中去。
5. clear () ,清除所有装载的键 -
值对。该方法在基类中提供。
------------------------------------------------------
二、操作properties文件的java方法
package com.hibernate.utils.util1; 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.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; public final class PropertiesUtil { /** * 读取某个属性的值 */ public static String getProperty(String fileName, String propertyName) { Properties prop = new Properties(); InputStream in = null; try { in = new FileInputStream(getFilePath() + fileName); prop.load(in); String propertyValue = prop.getProperty(propertyName); return propertyValue; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 增加或修改属性 */ public static void saveOrUpdateProperty(String fileName, String propertyName, String propertyValue) { Map<String, String> toSaveMap = getProperties(fileName); toSaveMap.put(propertyName, propertyValue);// 添加或更新属性 Properties prop = new Properties(); OutputStream out = null; try { out = new FileOutputStream(getFilePath() + fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } prop.putAll(toSaveMap); try { prop.store(out, "come from add Or update action");// 更新properties文件 } catch (IOException e) { e.printStackTrace(); } } /** * 删除属性 */ public static void deleteProperty(String fileName, String propertyName) { Map<String, String> toSaveMap = getProperties(fileName); toSaveMap.remove(propertyName);// 移除属性 Properties prop = new Properties(); OutputStream out = null; try { out = new FileOutputStream(getFilePath() + fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } prop.putAll(toSaveMap); try { prop.store(out, "");// 更新properties文件 } catch (IOException e) { e.printStackTrace(); } } /** * 获取类所在的绝对路径 */ public static String getFilePath() { return PropertiesUtil.class.getClass().getResource("/").getPath(); } /** * 读取properties文件的所有属性 */ public static Map<String, String> getProperties(String fileName) { Properties prop = new Properties(); try { InputStream in = new FileInputStream(getFilePath() + fileName); prop.load(in);// 加载属性值 } catch (IOException e) { e.printStackTrace(); } Map<String, String> toSaveMap = new HashMap<String, String>(); Set<Object> keys = prop.keySet(); Iterator<Object> it = keys.iterator(); while (it.hasNext()) { String key = (String) it.next(); String value = (String) prop.get(key); toSaveMap.put(key, value); } return toSaveMap; } }
----------------------------------------致青春----------------------------------------
标签:
原文地址:http://www.cnblogs.com/maxiaolang/p/4589237.html