欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 操作Properties配置文件詳解

 更新時(shí)間:2020年07月31日 15:33:20   作者:小顧問(wèn)  
這篇文章主要介紹了Java 操作Properties配置文件詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

java中的properties文件是一種配置文件,主要用于表達(dá)配置信息,文件類型為*.properties,格式為文本文件,文件的內(nèi)容是格式是"鍵=值"的格式,在properties

文件中,可以用"#"來(lái)作注釋,properties文件在Java編程中用到的地方很多,操作很方便。

一、properties文件

test.properties
------------------------------------------------------
#################################
# 工商報(bào)表應(yīng)用IcisReport的配置文件#
# 日期:2006年11月21日 #
#################################
#
# 說(shuō)明:業(yè)務(wù)系統(tǒng)TopIcis和報(bào)表系統(tǒng)IcisReport是分離的
# 可分開(kāi)部署到不同的服務(wù)器上,也可以部署到同一個(gè)服務(wù)
# 器上;IcisReprot作為獨(dú)立的web應(yīng)用程序可以使用任何
# 的Servlet容器或者J2EE服務(wù)器部署并單獨(dú)運(yùn)行,也可以
# 通過(guò)業(yè)務(wù)系統(tǒng)的接口調(diào)用作為業(yè)務(wù)系統(tǒng)的一個(gè)庫(kù)來(lái)應(yīng)用.
#
# IcisReport的ip
IcisReport.server.ip=192.168.3.143
# IcisReport的端口
IcisReport.server.port=8080
# IcisReport的上下文路徑
IcisReport.contextPath=/IcisReport

------------------------------------------------------

Properties類的重要方法

Properties類存在于胞Java.util 中,該類繼承自 Hashtable
1. getProperty ( String key) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過(guò)參數(shù) key ,得到 key 所對(duì)應(yīng)的 value。
2. load ( InputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對(duì))。通過(guò)對(duì)指定的文件(比如說(shuō)上面的 test.properties文件)進(jìn)行裝載來(lái)獲取該文

件中的所有鍵 - 值對(duì)。以供 getProperty ( String key) 來(lái)搜索。
3. setProperty ( String key, String value) ,調(diào)用 Hashtable 的方法 put 。他通過(guò)調(diào)用基類的put方法來(lái)設(shè)置 鍵 - 值對(duì)。
4. store ( OutputStream out, String comments) , 以適合使用 load 方法加載到Properties表中的格式,將此Properties表中的屬性列表(鍵和元素

對(duì))寫(xiě)入輸出流。與 load 方法相反,該方法將鍵 - 值對(duì)寫(xiě)入到指定的文件中去。
5. clear () ,清除所有裝載的 鍵 - 值對(duì)。該方法在基類中提供。
-------------------------------

二、操作properties文件的java方法

讀屬性文件
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/IcisReport.properties");
prop.load(in);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();)
{
String key = (String) it.next();
}
------------------------
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
-----------------------------------------------------------------------------------------
Class.getResourceAsStream ("/some/pkg/resource.properties");
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
java.util.ResourceBundle rs = java.util.ResourceBundle.getBundle("some.pkg.resource");
rs.getString("xiaofei");
-----------------------------------------------------------------------------------------
寫(xiě)屬性文件
Configuration saveCf = new Configuration();
saveCf.setValue("min", "10");
saveCf.setValue("max", "1000");
saveCf.saveFile(".\config\save.perperties","test");

總結(jié):java的properties文件需要放到classpath下面,這樣程序才能讀取到,有關(guān)classpath實(shí)際上就是java類或者庫(kù)的存放路徑,在java工程中,properties放到class文件一塊。在web應(yīng)用中,最簡(jiǎn)單的方法是放到web應(yīng)用的WEB- INF\classes目錄下即可,也可以放在其他文件夾下面,這時(shí)候需要在設(shè)置classpath環(huán)境變量的時(shí)候,將這個(gè)文件夾路徑加到 classpath變量中,這樣也也可以讀取到。在此,你需要對(duì)classpath有個(gè)深刻理解,classpath絕非系統(tǒng)中刻意設(shè)定的那個(gè)系統(tǒng)環(huán)境變量,WEB-INF\classes其實(shí)也是,java工程的class文件目錄也是。

發(fā)個(gè)例子大家自己看哈.

package control;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class TestMain {
 
 //根據(jù)key讀取value
 public static String readValue(String filePath,String key) {
 Properties props = new Properties();
    try {
     InputStream in = new BufferedInputStream (new FileInputStream(filePath));
     props.load(in);
     String value = props.getProperty (key);
      System.out.println(key+value);
      return value;
    } catch (Exception e) {
     e.printStackTrace();
     return null;
    }
 }
 
 //讀取properties的全部信息
  public static void readProperties(String filePath) {
   Properties props = new Properties();
    try {
     InputStream in = new BufferedInputStream (new FileInputStream(filePath));
     props.load(in);
      Enumeration en = props.propertyNames();
       while (en.hasMoreElements()) {
       String key = (String) en.nextElement();
          String Property = props.getProperty (key);
          System.out.println(key+Property);
        }
    } catch (Exception e) {
     e.printStackTrace();
    }
  }

  //寫(xiě)入properties信息
  public static void writeProperties(String filePath,String parameterName,String parameterValue) {
   Properties prop = new Properties();
   try {
   InputStream fis = new FileInputStream(filePath);
      //從輸入流中讀取屬性列表(鍵和元素對(duì))
      prop.load(fis);
      //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
      //強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
      OutputStream fos = new FileOutputStream(filePath);
      prop.setProperty(parameterName, parameterValue);
      //以適合使用 load 方法加載到 Properties 表中的格式,
      //將此 Properties 表中的屬性列表(鍵和元素對(duì))寫(xiě)入輸出流
      prop.store(fos, "Update '" + parameterName + "' value");
    } catch (IOException e) {
     System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
    }
  }

  public static void main(String[] args) {
   readValue("info.properties","url");
    writeProperties("info.properties","age","21");
    readProperties("info.properties" );
    System.out.println("OK");
  }

發(fā)個(gè)例子大家自己看哈.

package control;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class TestMain {
 
 //根據(jù)key讀取value
 public static String readValue(String filePath,String key) {
 Properties props = new Properties();
    try {
     InputStream in = new BufferedInputStream (new FileInputStream(filePath));
     props.load(in);
     String value = props.getProperty (key);
      System.out.println(key+value);
      return value;
    } catch (Exception e) {
     e.printStackTrace();
     return null;
    }
 }
 
 //讀取properties的全部信息
  public static void readProperties(String filePath) {
   Properties props = new Properties();
    try {
     InputStream in = new BufferedInputStream (new FileInputStream(filePath));
     props.load(in);
      Enumeration en = props.propertyNames();
       while (en.hasMoreElements()) {
       String key = (String) en.nextElement();
          String Property = props.getProperty (key);
          System.out.println(key+Property);
        }
    } catch (Exception e) {
     e.printStackTrace();
    }
  }

  //寫(xiě)入properties信息
  public static void writeProperties(String filePath,String parameterName,String parameterValue) {
   Properties prop = new Properties();
   try {
   InputStream fis = new FileInputStream(filePath);
      //從輸入流中讀取屬性列表(鍵和元素對(duì))
      prop.load(fis);
      //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
      //強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
      OutputStream fos = new FileOutputStream(filePath);
      prop.setProperty(parameterName, parameterValue);
      //以適合使用 load 方法加載到 Properties 表中的格式,
      //將此 Properties 表中的屬性列表(鍵和元素對(duì))寫(xiě)入輸出流
      prop.store(fos, "Update '" + parameterName + "' value");
    } catch (IOException e) {
     System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
    }
  }

  public static void main(String[] args) {
   readValue("info.properties","url");
    writeProperties("info.properties","age","21");
    readProperties("info.properties" );
    System.out.println("OK");
  }
}

到此這篇關(guān)于Java 操作Properties配置文件詳解的文章就介紹到這了,更多相關(guān)Java 操作Properties配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java并發(fā)中DelayQueue延遲隊(duì)列原理剖析

    java并發(fā)中DelayQueue延遲隊(duì)列原理剖析

    DelayQueue隊(duì)列是一個(gè)延遲隊(duì)列,本文將結(jié)合實(shí)例代碼,詳細(xì)的介紹DelayQueue延遲隊(duì)列的源碼分析,感興趣的小伙伴們可以參考一下
    2021-06-06
  • springboot添加AOP日志配置詳解

    springboot添加AOP日志配置詳解

    這篇文章主要介紹了springboot添加AOP日志配置詳解,日志是一種在軟件開(kāi)發(fā)中常用的技術(shù),用于記錄和跟蹤應(yīng)用程序的運(yùn)行過(guò)程,通過(guò)AOP日志,開(kāi)發(fā)人員可以實(shí)時(shí)監(jiān)控應(yīng)用程序的行為,包括方法的調(diào)用、參數(shù)的傳遞和返回值的獲取等,需要的朋友可以參考下
    2023-10-10
  • Java通過(guò)BCrypt加密過(guò)程詳解

    Java通過(guò)BCrypt加密過(guò)程詳解

    這篇文章主要介紹了Java通過(guò)BCrypt加密過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • RestTemplate自定義請(qǐng)求失敗異常處理示例解析

    RestTemplate自定義請(qǐng)求失敗異常處理示例解析

    這篇文章主要為大家介紹了RestTemplate自定義請(qǐng)求失敗異常處理的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • Java跳出當(dāng)前的多重嵌套循環(huán)的五種方法

    Java跳出當(dāng)前的多重嵌套循環(huán)的五種方法

    在Java編程中,跳出多重嵌套循環(huán)可以使用break語(yǔ)句、標(biāo)號(hào)與break組合、return語(yǔ)句、標(biāo)志變量和異常處理五種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • 詳解如何在SpringBoot中實(shí)現(xiàn)優(yōu)雅關(guān)閉

    詳解如何在SpringBoot中實(shí)現(xiàn)優(yōu)雅關(guān)閉

    這篇文章主要介紹了如何在SpringBoot中實(shí)現(xiàn)優(yōu)雅關(guān)閉,SpringBoot應(yīng)用程序的關(guān)閉可以是崩潰,也可以是手動(dòng)關(guān)閉的,Shutdown、Crash 和 Graceful 之間的區(qū)別在于,它控制決定了我們可以用這個(gè)事件做什么,本文中,一起研究下Spring Boot提供的開(kāi)箱即用功能之一:優(yōu)雅關(guān)閉
    2024-09-09
  • linux下idea、pycharm等輸入中文拼音時(shí)滿3個(gè)字母后無(wú)法繼續(xù)拼音輸入的問(wèn)題

    linux下idea、pycharm等輸入中文拼音時(shí)滿3個(gè)字母后無(wú)法繼續(xù)拼音輸入的問(wèn)題

    這篇文章主要介紹了linux下idea、pycharm等輸入中文拼音時(shí)滿3個(gè)字母后無(wú)法繼續(xù)拼音輸入的問(wèn)題,本文通過(guò)圖文并茂的形式給大家分享解決方法,需要的朋友可以參考下
    2021-04-04
  • Java之類加載機(jī)制案例講解

    Java之類加載機(jī)制案例講解

    這篇文章主要介紹了Java之類加載機(jī)制案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Kotlin 單例實(shí)例詳解

    Kotlin 單例實(shí)例詳解

    這篇文章主要介紹了Kotlin 單例實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI)

    淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI)

    這篇文章主要介紹了淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10

最新評(píng)論