最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 操作Properties配置文件詳解

 更新時間:2017年05月06日 14:34:23   作者:象  
本篇文章主要介紹了Java 操作Properties配置文件詳解,詳細的介紹了Properties和主要方法,有興趣的可以了解下

1 簡介:

JDK提供的java.util.Properties類繼承自Hashtable類并且實現(xiàn)了Map接口,是使用一種鍵值對的形式來保存屬性集,其中鍵和值都是字符串類型。

java.util.Properties類提供了getProperty()和setProperty()方法來操作屬性文件,同時使用load()方法和store()方法加載和保存Properties配置文件。

java.util.ResourceBundle類也提供了讀取Properties配置文件的方法,ResourceBundle是一個抽象類。

2.Properties中的主要方法

1)load(InputStream inStream):該方法可以從.properties屬性文件對應(yīng)的文件數(shù)入流中,加載屬性列表到Properties類對象中。load有兩個方法的重載:load(InputStream inStream)、load(Reader reader),可根據(jù)不同的方式來加載屬性文件。

InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("demo.properties"); 
//通過當(dāng)前類加載器的getResourceAsStream方法獲取
//TestProperties當(dāng)前類名;TestProperties.class.取得當(dāng)前對象所屬的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()靜態(tài)方法來獲取,此方法獲取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"); 

在使用中遇到的問題可能是配置文件的路徑,當(dāng)配置文件不在當(dāng)前類所在的包下,則需要使用包名限定;若屬性文件在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 ;
  
  /**
   * 默認構(gòu)造函數(shù)
   */
  public PropsUtil() {}
  
  /**
   * 構(gòu)造函數(shù)
   * @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();
    }
  }
  
  /**
   * 通過關(guān)鍵字獲取值
   * @param key
   * @return 返回對應(yīng)的字符串,如果無,返回null
   */
  public String getValueByKey(String key) {
    if(properties==null)
      properties = loadProps();
    String val = properties.getProperty(key.trim());
    return val;
  }
  
  /**
   * 通過關(guān)鍵字獲取值
   * @param key 需要獲取的關(guān)鍵字
   * @param defaultValue 若找不到對應(yīng)的關(guān)鍵字時返回的值
   * @return 返回找到的字符串
   */
  public String getValueByKey(String key,String defaultValue){
    if(properties==null)
      properties = loadProps();
    return properties.getProperty(key, defaultValue);
  }
  
  /**
   * 獲取Properties所有的值
   * @return 返回Properties的鍵值對
   */
  public Map<String, String> getAllProperties() {
    if(properties==null)
      properties = loadProps();
    Map<String, String> map = new HashMap<String, String>();
    // 獲取所有的鍵值
    Iterator<String> 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 對應(yīng)的鍵
   * @param value 對應(yīng)的值
   */
  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 對應(yīng)的鍵
   * @param value 對應(yīng)的值
   */
   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");
    }
   }
   
   /**
   * 設(shè)置path值
   * @param path
   */
   public void setPath(String path){
     this.path = path;
   }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于eclipse-temurin鏡像部署spring boot應(yīng)用的實現(xiàn)示例

    基于eclipse-temurin鏡像部署spring boot應(yīng)用的實現(xiàn)示例

    本文提供了基于eclipse-temurin鏡像部署Spring Boot應(yīng)用的詳細實現(xiàn)示例,通過使用Docker鏡像,可以輕松地創(chuàng)建和管理Spring Boot應(yīng)用程序的容器化環(huán)境,感興趣的可以了解一下
    2023-08-08
  • 接口重試的7種常用方案詳細介紹

    接口重試的7種常用方案詳細介紹

    這篇文章主要為大家詳細介紹了接口重試的7種常用方案,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以根據(jù)需求選擇
    2025-03-03
  • 利用Jackson實現(xiàn)數(shù)據(jù)脫敏的示例詳解

    利用Jackson實現(xiàn)數(shù)據(jù)脫敏的示例詳解

    在我們的企業(yè)項目中,為了保護用戶隱私,數(shù)據(jù)脫敏成了必不可少的操作,那么我們怎么優(yōu)雅的利用Jackson實現(xiàn)數(shù)據(jù)脫敏呢,本文就來和大家詳細聊聊,希望對大家有所幫助
    2023-05-05
  • Java列表元素自定義排序方式

    Java列表元素自定義排序方式

    文章介紹了在Java開發(fā)中如何對列表元素進行自定義排序,通過實現(xiàn)`Comparator`接口并重寫`compare`方法來指定自定義排序規(guī)則,示例展示了如何對漢字數(shù)字進行排序,并通過改變自定義順序列表的元素添加順序來實現(xiàn)倒序排序
    2024-12-12
  • Spring IOC (DI) 依賴注入的四種方式示例詳解

    Spring IOC (DI) 依賴注入的四種方式示例詳解

    這篇文章主要介紹了Spring IOC (DI) 依賴注入的四種方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • java判斷字符串是否為數(shù)字的方法小結(jié)

    java判斷字符串是否為數(shù)字的方法小結(jié)

    這篇文章主要介紹了java判斷字符串是否為數(shù)字的方法,分別講述了使用Java自帶函數(shù)、正則表達式及ascii碼三種方法進行字符串判斷的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Java中實現(xiàn)String.padLeft和String.padRight的示例

    Java中實現(xiàn)String.padLeft和String.padRight的示例

    本篇文章主要介紹了Java中實現(xiàn)String.padLeft和String.padRight,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java為何需要平衡方法調(diào)用與內(nèi)聯(lián)

    Java為何需要平衡方法調(diào)用與內(nèi)聯(lián)

    這篇文章主要介紹了Java為何需要平衡方法調(diào)用與內(nèi)聯(lián),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-01-01
  • SpringBoot最簡潔的國際化配置

    SpringBoot最簡潔的國際化配置

    這篇文章主要介紹了SpringBoot最簡潔的國際化配置,Spring Boot是一個用于構(gòu)建獨立的、生產(chǎn)級別的Spring應(yīng)用程序的框架,國際化是一個重要的功能,它允許應(yīng)用程序根據(jù)用戶的語言和地區(qū)顯示不同的內(nèi)容,在Spring Boot中,實現(xiàn)國際化非常簡單,需要的朋友可以參考下
    2023-10-10
  • Spring創(chuàng)建bean實例的幾種方式分享

    Spring創(chuàng)建bean實例的幾種方式分享

    這篇文章主要介紹了Spring創(chuàng)建bean實例的幾種方式分享,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07

最新評論

新龙县| 沐川县| 崇仁县| 同德县| 望谟县| 哈密市| 太谷县| 遵义县| 香河县| 岳普湖县| 安陆市| 英山县| 利川市| 浦城县| 山东省| 河东区| 康马县| 怀宁县| 高清| 五台县| 丹东市| 乾安县| 临安市| 东安县| 尚义县| 沽源县| 绥江县| 建水县| 石林| 大悟县| 深泽县| 台北市| 宁城县| 星座| 涞源县| 泊头市| 元朗区| 黔西| 江华| 韩城市| 光山县|