properties 配置 java_Java 操作Properties配置文件详解
1 簡介:
JDK提供的java.util.Properties類繼承自Hashtable類并且實現了Map接口,是使用一種鍵值對的形式來保存屬性集,其中鍵和值都是字符串類型。
java.util.Properties類提供了getProperty()和setProperty()方法來操作屬性文件,同時使用load()方法和store()方法加載和保存Properties配置文件。
java.util.ResourceBundle類也提供了讀取Properties配置文件的方法,ResourceBundle是一個抽象類。
2.Properties中的主要方法
1)load(InputStream inStream):該方法可以從.properties屬性文件對應的文件數入流中,加載屬性列表到Properties類對象中。load有兩個方法的重載:load(InputStream inStream)、load(Reader reader),可根據不同的方式來加載屬性文件。
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("demo.properties");
//通過當前類加載器的getResourceAsStream方法獲取
//TestProperties當前類名;TestProperties.class.取得當前對象所屬的Class對象; getClassLoader():取得該Class對象的類裝載器
InputStream in = ClassLoader.getSystemResourceAsStream("filePath");
InputStream inStream = new FileInputStream(new File("filePath")); //從文件獲取
InputStream in = context.getResourceAsStream("filePath"); //在servlet中,可以通過context來獲取InputStream
InputStream inStream = new URL("path").openStream(); //通過URL來獲取
讀取方法如下:
Properties pro = new Properties(); //實例化一個Properties對象
InputStream inStream = new FileInputStream("demo.properties"); //獲取屬性文件的文件輸入流
pro.load(nStream);
inStream.close();
2)store(OutputStream out,String comments):這個方法將Properties類對象的屬性列表寫入.properties配置文件。如下:
FileOutputStream outStream = new FileOutputStream("demo.properties");
pro.store(outStream,"Comment");
outStream.close();
3 ResourceBundle中的主要方法
通過ResourceBundle.getBundle()靜態方法來獲取,此方法獲取properties屬性文件不需要加.properties后綴名。也可以從InputStream中獲取ResourceBundle對象。
ResourceBundle resource = ResourceBundle.getBundle("com/xiang/demo");//emo為屬性文件名,放在包com.xiang下,如果是放在src下,直接用test即可
ResourceBundle resource1 = new PropertyResourceBundle(inStream);
String value = resource.getString("name");
在使用中遇到的問題可能是配置文件的路徑,當配置文件不在當前類所在的包下,則需要使用包名限定;若屬性文件在src根目錄下,則直接使用demo.properties或demo即可。
4 Properties操作實例
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* Java中Preperties配置文件工具類
* @author shu
*
*/
public class PropsUtil {
private String path = "";
private Properties properties ;
/**
* 默認構造函數
*/
public PropsUtil() {}
/**
* 構造函數
* @param path 傳入Properties地址值
*/
public PropsUtil(String path) {
this.path = path;
}
/**
* 加載properties文件
* @return 返回讀取到的properties對象
*/
public Properties loadProps(){
InputStream inStream = ClassLoader.getSystemResourceAsStream(path);
try {
if(inStream==null)
throw new FileNotFoundException(path + " file is not found");
properties = new Properties();
properties.load(inStream);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 將配置寫入到文件
*/
public void writeFile(){
// 獲取文件輸出流
try {
FileOutputStream outputStream = new FileOutputStream( new File(ClassLoader.getSystemResource(path).toURI()));
properties.store(outputStream, null);
outputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通過關鍵字獲取值
* @param key
* @return 返回對應的字符串,如果無,返回null
*/
public String getValueByKey(String key) {
if(properties==null)
properties = loadProps();
String val = properties.getProperty(key.trim());
return val;
}
/**
* 通過關鍵字獲取值
* @param key 需要獲取的關鍵字
* @param defaultValue 若找不到對應的關鍵字時返回的值
* @return 返回找到的字符串
*/
public String getValueByKey(String key,String defaultValue){
if(properties==null)
properties = loadProps();
return properties.getProperty(key, defaultValue);
}
/**
* 獲取Properties所有的值
* @return 返回Properties的鍵值對
*/
public Map getAllProperties() {
if(properties==null)
properties = loadProps();
Map map = new HashMap();
// 獲取所有的鍵值
Iterator it=properties.stringPropertyNames().iterator();
while(it.hasNext()){
String key=it.next();
map.put(key, properties.getProperty(key));
}
/*Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
String value = getValueByKey(key);
map.put(key, value);
}*/
return map;
}
/**
* 往Properties寫入新的鍵值且保存
* @param key 對應的鍵
* @param value 對應的值
*/
public void addProperties(String key, String value) {
if(properties==null)
properties = loadProps();
properties.setProperty(key, value);
try {
writeFile();
} catch (Exception e) {
throw new RuntimeException("write fail");
}
}
/**
* 更新配置文件
* @param key 對應的鍵
* @param value 對應的值
*/
public void update(String key,String value){
if(properties==null)
properties = loadProps();
if(properties.containsKey(key))
properties.replace(key, value);
try {
writeFile();
} catch (Exception e) {
throw new RuntimeException("write fail");
}
}
/**
* 刪除某一鍵值對
* @param key
*/
public void deleteByKey(String key){
if(properties==null)
properties = loadProps();
if(!properties.containsKey(key))
throw new RuntimeException("not such key");
properties.remove(key);
try {
writeFile();
} catch (Exception e) {
throw new RuntimeException("write fail");
}
}
/**
* 設置path值
* @param path
*/
public void setPath(String path){
this.path = path;
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的properties 配置 java_Java 操作Properties配置文件详解的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python应用于期货_Python期货
- 下一篇: java 静态对象数组_Java静态方法
