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

Java 操作Properties配置文件詳解

 更新時間:2020年07月31日 15:33:20   作者:小顧問  
這篇文章主要介紹了Java 操作Properties配置文件詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

一、properties文件

test.properties
------------------------------------------------------
#################################
# 工商報表應用IcisReport的配置文件#
# 日期:2006年11月21日 #
#################################
#
# 說明:業(yè)務系統(tǒng)TopIcis和報表系統(tǒng)IcisReport是分離的
# 可分開部署到不同的服務器上,也可以部署到同一個服務
# 器上;IcisReprot作為獨立的web應用程序可以使用任何
# 的Servlet容器或者J2EE服務器部署并單獨運行,也可以
# 通過業(yè)務系統(tǒng)的接口調用作為業(yè)務系統(tǒ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) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數(shù) key ,得到 key 所對應的 value。
2. load ( InputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties文件)進行裝載來獲取該文

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

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

二、操作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");
-----------------------------------------------------------------------------------------
寫屬性文件
Configuration saveCf = new Configuration();
saveCf.setValue("min", "10");
saveCf.setValue("max", "1000");
saveCf.saveFile(".\config\save.perperties","test");

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

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

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();
    }
  }

  //寫入properties信息
  public static void writeProperties(String filePath,String parameterName,String parameterValue) {
   Properties prop = new Properties();
   try {
   InputStream fis = new FileInputStream(filePath);
      //從輸入流中讀取屬性列表(鍵和元素對)
      prop.load(fis);
      //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
      //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
      OutputStream fos = new FileOutputStream(filePath);
      prop.setProperty(parameterName, parameterValue);
      //以適合使用 load 方法加載到 Properties 表中的格式,
      //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
      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ā)個例子大家自己看哈.

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();
    }
  }

  //寫入properties信息
  public static void writeProperties(String filePath,String parameterName,String parameterValue) {
   Properties prop = new Properties();
   try {
   InputStream fis = new FileInputStream(filePath);
      //從輸入流中讀取屬性列表(鍵和元素對)
      prop.load(fis);
      //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
      //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
      OutputStream fos = new FileOutputStream(filePath);
      prop.setProperty(parameterName, parameterValue);
      //以適合使用 load 方法加載到 Properties 表中的格式,
      //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
      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");
  }
}

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

相關文章

  • JAVA調用JavaScript方法舉例詳解

    JAVA調用JavaScript方法舉例詳解

    之前在一次機緣巧合的情況下,需要時用JAVA執(zhí)行js方法,查閱了一些文檔,找到了相關解決方法,這里和大家分享一下,下面這篇文章主要給大家介紹了關于JAVA調用JavaScript方法的相關資料,需要的朋友可以參考下
    2023-10-10
  • Java開發(fā)者必備10大數(shù)據(jù)工具和框架

    Java開發(fā)者必備10大數(shù)據(jù)工具和框架

    這篇文章主要為大家詳細介紹了Java開發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Json 自定義使用函數(shù)的簡單實例

    Json 自定義使用函數(shù)的簡單實例

    下面小編就為大家?guī)硪黄狫son 自定義使用函數(shù)的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • JDK8通過Stream 對List,Map操作和互轉的實現(xiàn)

    JDK8通過Stream 對List,Map操作和互轉的實現(xiàn)

    這篇文章主要介紹了JDK8通過Stream 對List,Map操作和互轉的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Java 數(shù)據(jù)結構與算法系列精講之背包問題

    Java 數(shù)據(jù)結構與算法系列精講之背包問題

    背包問題是一個非常典型的考察動態(tài)規(guī)劃應用的題目,對其加上不同的限制和條件,可以衍生出諸多變種,若要全面理解動態(tài)規(guī)劃,就必須對背包問題了如指掌
    2022-02-02
  • Java自定義數(shù)組列表的實現(xiàn)操作

    Java自定義數(shù)組列表的實現(xiàn)操作

    這篇文章主要介紹了Java自定義數(shù)組列表的實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot統(tǒng)一數(shù)據(jù)返回的幾種方式

    SpringBoot統(tǒng)一數(shù)據(jù)返回的幾種方式

    在Web應用程序開發(fā)中,統(tǒng)一數(shù)據(jù)返回格式對于前后端分離項目尤為重要,本文就來介紹一下SpringBoot統(tǒng)一數(shù)據(jù)返回的幾種方式,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • 配置java.library.path加載庫文件問題

    配置java.library.path加載庫文件問題

    這篇文章主要介紹了配置java.library.path加載庫文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java中List轉Map的幾種具體實現(xiàn)方式和特點

    Java中List轉Map的幾種具體實現(xiàn)方式和特點

    這篇文章主要介紹了幾種常用的List轉Map的方式,包括使用for循環(huán)遍歷、Java8StreamAPI、ApacheCommonsCollections和GoogleGuava,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-01-01
  • Java如何有效避免SQL注入漏洞的方法總結

    Java如何有效避免SQL注入漏洞的方法總結

    SQL注入是比較常見的網(wǎng)絡攻擊方式之一,它不是利用操作系統(tǒng)的BUG來實現(xiàn)攻擊,而是針對程序員編程時的疏忽,通過SQL語句,實現(xiàn)無帳號登錄,甚至篡改數(shù)據(jù)庫,這篇文章主要給大家介紹了關于Java如何避免SQL注入漏洞的兩種方法,需要的朋友可以參考下
    2022-01-01

最新評論

金秀| 连州市| 洛南县| 奇台县| 苗栗市| 文水县| 隆德县| 济源市| 许昌县| 陕西省| 万安县| 新平| 镇远县| 镇平县| 柘城县| 固原市| 高阳县| 博兴县| 湛江市| 河南省| 漾濞| 郯城县| 临江市| 封开县| 通州区| 五常市| 萨嘎县| 定西市| 光泽县| 七台河市| 和龙市| 剑阁县| 张家港市| 兴海县| 盐边县| 无为县| 讷河市| 浮山县| 潍坊市| 马山县| 临海市|