java properties配置文件操作

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

配置文件實例:如debug.properties 

#Tue Mar 21 15:46:17 CST 2017

#key=value

remote.debug.prot=7451


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

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

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

 

public static File getFile (String filePath){

    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){

    try(InputStream in =  new BufferedInputStream (

    new FileInputStream(PropertiesUtil.getFile(filePath)))){ 

            Properties pps = new Properties();

            pps.load(in);      

            return pps.getProperty(key);

        }catch (Exception e) {

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

            return null;

        }

    }

    

2.2得到所有HashMap<key,value>方法

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

//-return-  HashMap<String, String>: HashMap<key, value>--

   

public static Map<String, String> getAllProperties(String filePath){

        Map<String, String> proper = new HashMap<String, String>();

    try(InputStream in = new BufferedInputStream(

    new FileInputStream(PropertiesUtil.getFile(filePath)))){

       Properties pps = new Properties(); 

       pps.load(in);

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

       while(en.hasMoreElements()) {

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

           String strValue = pps.getProperty(strKey);

           proper.put(strKey, strValue);

       }

      

       return proper;

    }catch(Exception e)

    {

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

    return null;

    }

    }

2.3根據key修改value值方法

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

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

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

//-return- 狀態值--

   

 

public static String setValueByKey (String filePath, String pKey, String pValue){

    Properties pps = new Properties();    

    try(InputStream in =  new FileInputStream(PropertiesUtil.getFile(filePath))){  

       pps.load(in);

       pps.setProperty(pKey, pValue);

    }catch(Exception e)

        {

         //LOG.error("打開配置文件失敗", e);

        return null;

        }

    try(OutputStream out =  new FileOutputStream(PropertiesUtil.getFile(filePath))){

       pps.store(out, "");

       return TRUE;

    }catch(Exception e)

   {

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

      return null;

   }

    }

2.3寫入所有key,value值方法

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

//-in- HashMap<String, String>:  鍵值對HashMap<key, value>如remote.debug.prot=900--

//-return- 狀態值--

   

 

public static String setAllProperties(String filePath,Map<String, String> kv) {

    Properties pps = new Properties();

    try(InputStream in = new FileInputStream(PropertiesUtil.getFile(filePath))){

            pps.load(in);

           

            Iterator<Entry<String, String>> 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);

            }

    }catch(Exception e)

    {

    //LOG.error("打開配置文件失敗", e);

    return null;

    }

        try(OutputStream out = new FileOutputStream(PropertiesUtil.getFile(filePath))){

        pps.store(out, "");     

       return TRUE;

        }catch(Exception e)

    {

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

    return null;

    }

    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章