日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java过去配置文件的值_java对.properties配置文件操作

發布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java过去配置文件的值_java对.properties配置文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現運用Java.util.Properties來進行對.properties配置文件操作。

配置文件實例:如debug.properties

#Tue Mar 21 15:46:17 CST 2017

#key=value

remote.debug.prot=7451

第一步寫個獲取文件路徑的外部方法

//-in- String filePath:配置文件名如debug.properties--

//-return- 文件類對象--

public static File getFile (String filePath) throws IOException{

try{

ClassLoader classLoader = Thread.currentThread()

.getContextClassLoader();

URL url = classLoader.getResource(filePath);

return new File(url.toURI());

}catch(Exception e){

LOG.error("配置文件錯誤",e);

return null;

}

}

2.1根據key獲取value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in- key:鍵值如remote.debug.prot--

//-return- value值--

public static String getValueByKey(String filePath, String key) throws IOException{

try {

File fileURI = PropertiesUtil.getFile(filePath);

InputStream in = new BufferedInputStream (new FileInputStream(fileURI));

Properties pps = new Properties();

pps.load(in);

String value = pps.getProperty(key);

return value;

}catch (Exception e) {

LOG.error("初始化配置文件失敗",e);

return null;

}

}

2.2得到所有HashMap方法

//-in- String filePath:配置文件名如debug.properties--

//-return- ?HashMap:?HashMap--

public static HashMap getAllProperties(String filePath) throws IOException {

HashMap proper = new HashMap();

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

InputStream in = new BufferedInputStream(new FileInputStream(fileURI));

pps.load(in);

Enumeration> en = pps.propertyNames(); //得到配置文件的名字

while(en.hasMoreElements()) {

String strKey = (String) en.nextElement();

String strValue = pps.getProperty(strKey);

proper.put(strKey, strValue);

}

}catch(Exception e)

{

LOG.error("獲取所有配置文件信息失敗",e);

return null;

}

return proper;

}

2.3根據key獲取value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in- pKey: ?鍵值如remote.debug.prot--

//-in- pValue: 結果值如 2000--

//-return- 狀態值--

public static String writeProperties (String filePath, String pKey, String pValue) throws IOException {

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

InputStream in = new FileInputStream(fileURI);

pps.load(in);

OutputStream out = new FileOutputStream(fileURI);

pps.setProperty(pKey, pValue);

pps.store(out, "");

return TRUE;

}catch(Exception e)

{

LOG.error("插入配置文件失敗", e);

return null;

}

}

2.3寫入所有key,value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in-?HashMap: ?鍵值對HashMap如remote.debug.prot=900--

//-return- 狀態值--

public static String setAllProperties(String filePath,HashMap kv) throws IOException {

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

InputStream in = new FileInputStream(fileURI);

pps.load(in);

OutputStream out = new FileOutputStream(fileURI);

Iterator> iter = kv.entrySet().iterator();

while (iter.hasNext()) {

@SuppressWarnings("rawtypes")

Map.Entry entry = (Map.Entry) iter.next();

Object key = entry.getKey();

Object val = entry.getValue();

pps.setProperty((String)key, (String)val);

pps.store(out, "");

}

}catch(Exception e)

{

return null;

}

return TRUE;

}

總結

以上是生活随笔為你收集整理的java过去配置文件的值_java对.properties配置文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。