Java工具類---資源文件解析類 PropertiesUtil
package com.luang.util.properties; import java.io.File; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.OutputStream;
import java.net.URI; import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;/**
- PropertiesUtil.java *
- @desc properties 資源文件解析工具
- @author Guoxp
@datatime Apr 7, 2013 3:58:45 PM / public class PropertiesUtil {
private Properties props;
private URI uri;public PropertiesUtil(String fileName){
readProperties(fileName);
}
private void readProperties(String fileName) {try { props = new Properties(); InputStream fis =getClass().getResourceAsStream(fileName); props.load(fis); uri = this.getClass().getResource("/dbConfig.properties").toURI(); } catch (Exception e) { e.printStackTrace(); }
}
/**- 獲取某個屬性
*/
public String getProperty(String key){
return props.getProperty(key);
}
/** - 獲取所有屬性,返回一個map,不常用
- 可以試試props.putAll(t)
*/
public Map getAllProperty(){
Map map=new HashMap();
Enumeration enu = props.propertyNames();
while (enu.hasMoreElements()) {
}String key = (String) enu.nextElement(); String value = props.getProperty(key); map.put(key, value);
return map;
}
/** - 在控制臺上打印出所有屬性,調試時用。
*/
public void printProperties(){
props.list(System.out);
}
/** - 寫入properties信息
*/
public void writeProperties(String key, String value) {
try {
OutputStream fos = new FileOutputStream(new File(uri));
} catch (Exception e) {props.setProperty(key, value); // 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流 props.store(fos, "『comments』Update key:" + key);
e.printStackTrace(); }
}
public static void main(String[] args) {
PropertiesUtil util=new PropertiesUtil("src/dbConfig.properties");
util.writeProperties("dbtype", "MSSQL");
}
} </pre>
- 獲取某個屬性
*/
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!