一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java 操作Properties配置文件詳解(二)

Java 操作Properties配置文件詳解(二)

2020-09-25 14:13 JAVA教程

本篇文章主要介紹了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ù)不同的方式來加載屬性文件。

?
1
2
3
4
5
6
7
8
9
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來獲取

讀取方法如下:

?
1
2
3
4
Properties pro = new Properties();                   //實例化一個Properties對象
InputStream inStream = new FileInputStream("demo.properties");     //獲取屬性文件的文件輸入流
pro.load(nStream);
inStream.close();

 2)store(OutputStream out,String comments):這個方法將Properties類對象的屬性列表寫入.properties配置文件。如下:

?
1
2
3
FileOutputStream outStream = new FileOutputStream("demo.properties");
pro.store(outStream,"Comment");
outStream.close();

3 ResourceBundle中的主要方法

 通過ResourceBundle.getBundle()靜態(tài)方法來獲取,此方法獲取properties屬性文件不需要加.properties后綴名。也可以從InputStream中獲取ResourceBundle對象。

?
1
2
3
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操作實例

?
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/xiangBlog/p/6816697.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: japanese超丰满人妖 | 成人xxxxxx| 四虎影库紧急大通知 | 99久久精品国产片久人 | 好姑娘完整版在线观看中文 | 男人把大ji巴放进男人免费视频 | 国产成人一区二区三区影院免费 | 激情婷婷综合久久久久 | 操国产美女 | 人人人人人看碰人人免费 | 国产a片毛片 | 841995论坛网站2022年 | 日本漫画无翼乌 | 非洲一级毛片又粗又长aaaa | 波多野结衣两女调教 | 日本一区二区三区四区无限 | 色帝国亚洲欧美在线蜜汁tv | 日本精品一区二区在线播放 | 非洲黑人gay巨大 | 高清色黄毛片一级毛片 | 色呦呦tv | 国产美女亚洲精品久久久综合91 | 成人久久18免费网站入口 | 亚洲欧美日韩国产综合专区 | 插入肥臀 | 国产大乳美女挤奶视频 | 国产女同精品 | 天天干夜夜玩 | 大伊人青草狠狠久久 | 亚洲AV永久无码精品澳门 | 精品91自产拍在线观看99re | 青青草99热这里都是精品 | 国语精彩对白2021 | 色婷婷狠狠 | 激情视频网址 | 欧美一级在线播放 | 无码人妻丰满熟妇啪啪网不卡 | 好看的亚洲视频 | 国产欧美日韩精品高清二区综合区 | 青青操在线 | 国产亚洲精品视频中文字幕 |