激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 操作Properties配置文件詳解

Java 操作Properties配置文件詳解

2020-08-01 14:36小顧問 Java教程

這篇文章主要介紹了Java 操作Properties配置文件詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

一、properties文件

test.properties
------------------------------------------------------
#################################
# 工商報表應用IcisReport的配置文件#
# 日期:2006年11月21日 #
#################################
#
# 說明:業務系統TopIcis和報表系統IcisReport是分離的
# 可分開部署到不同的服務器上,也可以部署到同一個服務
# 器上;IcisReprot作為獨立的web應用程序可以使用任何
# 的Servlet容器或者J2EE服務器部署并單獨運行,也可以
# 通過業務系統的接口調用作為業務系統的一個庫來應用.
#
# 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) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 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方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
讀屬性文件
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環境變量的時候,將這個文件夾路徑加到 classpath變量中,這樣也也可以讀取到。在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變量,WEB-INF\classes其實也是,java工程的class文件目錄也是。

發個例子大家自己看哈.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 {
 
 //根據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");
  }

發個例子大家自己看哈.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 {
 
 //根據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配置文件內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: xxxeexxx性国产| 免费三级大片 | 国产精品久久久久久久久久了 | 女人一级一级毛片 | 一色视频 | 中文字幕综合在线观看 | 日韩精品免费一区二区三区 | 亚洲一区成人 | 极品一级片 | 一级性色 | 黄色免费播放网站 | 国内精品久久久久久影视8 嫩草影院在线观看网站成人 | 欧美成人理论片乱 | 密室逃脱第一季免费观看完整在线 | 欧美hdfree性xxxx | 逼特逼视频在线观看 | 国产一级二级视频 | 伊久在线 | 成人男女视频 | 亚洲免费视频一区二区 | 国产毛片视频 | 成人三级视频网站 | 99国内精品 | 一区二区久久久久草草 | 福利在线小视频 | 免费看成人毛片 | 久久久久久久九九九九 | 亚洲人成在线播放网站 | 成人一级视频 | 日本久久视频 | 成人免费一区二区三区在线观看 | 国产精品久久久久久久久久iiiii | 国产精品爱久久久久久久 | 人成免费a级毛片 | 色七七网站 | 久久伊人精品热在75 | 久久久久se| 一级尻逼视频 | 黄色片免费在线播放 | 在线观看中文字幕av | 91亚洲免费视频 |