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

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

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

服務器之家 - 編程語言 - Java教程 - Java 對 Properties 文件的操作詳解及簡單實例

Java 對 Properties 文件的操作詳解及簡單實例

2020-08-12 10:49lqh Java教程

這篇文章主要介紹了Java 對 Properties 文件的操作詳解及簡單實例的相關資料,需要的朋友可以參考下

JavaProperties 文件的操作

簡介

在 Java 中,我們常用 java.util.Properties.Properties 類來解析 Properties 文件,Properties 格式文件是 Java 常用的配置文件,它用來在文件中存儲鍵-值對,其中鍵和值用等號分隔,格式如下:

?
1
name=shawearn

Properties 類是 java.util.Hashtable<Object, Object> 的子類,用于鍵和值之間的映射。

在對 Properties 格式文件的操作中,我們常使用 Properties 類的一下方法:

Properties():用于創建一個無任何屬性值 Properties 對象;

  • void load(InputStream inStream):從輸入流中加載屬性列表;
  • void store(OutputStream out, String comments):根據輸出流將屬性列表保存到文件中;
  • String  getProperty(String key):獲取指定鍵的值;
  • void setProperty(String key, String value):設置指定鍵的值,若指定鍵已經在原屬性值列表中存在,則覆蓋;若指定鍵在原屬性值列表中不存在,則新增;

寫入 Properties 文件:

?
1
2
3
4
5
6
7
8
9
// 創建一個 Properties 實例;
Properties p = new Properties();
// 為 Properties 設置屬性及屬性值;
p.setProperty("name", "shawearn");
p.setProperty("address", "XX 省 XX 市");
// 保存 Properties 到 shawearn.properties 文件中;
FileOutputStream out = new FileOutputStream("shawearn.properties");
p.store(out, "Create by Shawearn!");
out.close();

讀取 Properties 文件:

?
1
2
3
4
5
6
7
// 創建一個 Properties 實例;
Properties p = new Properties();
// 讀取配置文件;
FileInputStream in = new FileInputStream("shawearn.properties");
// 加載配置文件到 Properties 實例中;
p.load(in);
in.close();

最后附上測試代碼:

?
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
package com.shawearn.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
 * @author Shawearn
 *
 */
public class TestProperties {
  /**
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    TestProperties t = new TestProperties();
    // 測試寫入;
    t.testWrite();
    // 測試讀取;
    t.testRead();
  }
  /*
   * 測試對 Properties 文件的寫入操作;
   */
  private void testWrite() throws IOException {
    // 創建一個 Properties 實例;
    Properties p = new Properties();
    // 為 Properties 設置屬性及屬性值;
    p.setProperty("name", "shawearn");
    p.setProperty("address", "XX 省 XX 市");
    // 保存 Properties 到 shawearn.properties 文件中;
    FileOutputStream out = new FileOutputStream("shawearn.properties");
    p.store(out, "Create by Shawearn!");
    out.close();
    System.out.println("寫入成功!");
  }
  /*
   * 測試對 Properties 文件的讀取操作;
   */
  private void testRead() throws IOException {
    // 創建一個 Properties 實例;
    Properties p = new Properties();
    // 讀取配置文件;
    FileInputStream in = new FileInputStream("shawearn.properties");
    // 加載配置文件到 Properties 實例中;
    p.load(in);
    in.close();
    // 獲取 Properties 文件中所有的 key;
    Set<String> keys = p.stringPropertyNames();
    // 遍歷所有的 key;
    for (String key : keys) {
      // 獲取 Properties 文件中 key 所對應的 value;
      Object value = p.get(key);
      // 輸入 key 和對應的 value;
      System.out.println(key + " => " + value);
    }
  }
}

控制臺輸出結果:

?
1
2
address => XX 省 XX 市
name => shawearn

shawearn.properties 文件內容:

?
1
2
3
4
#Create by Shawearn!
#Thu Nov 19 12:43:41 CST 2015
name=shawearn
address=XX \u7701 XX \u5E02

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天堂成人国产精品一区 | 久久久一区二区精品 | 亚洲 综合 欧美 动漫 丝袜图 | av日韩一区二区三区 | 欧美一级做一a做片性视频 日韩黄色片免费看 | 中文字幕xxx | 在线观看免费污视频 | 免费在线观看午夜视频 | 日本在线观看视频网站 | 日本在线播放一区二区三区 | 精品一区二区三区在线视频 | 精品国产91久久久久久久妲己 | 免费成人 | www亚洲| 美女av在线免费观看 | 精品亚洲在线 | 一区二区三区黄色 | 久久久久久久久成人 | 成人免费福利 | 欧美日韩亚洲视频 | 玩偶姐姐 在线观看 | 久久精品av | 国产午夜精品久久久久久免费视 | 成人激情视频网站 | 国产高潮好爽受不了了夜色 | 国产九九热视频 | 欧美大电影免费观看 | 国产二区三区在线播放 | av免费片| 九九热九九热 | 欧美一级做性受免费大片免费 | 女人一级一级毛片 | 日日夜av| 性色av一区二区三区在线观看 | 国产宾馆3p国语对白 | 免费永久看羞羞片网站入口 | 国产精品欧美日韩一区二区 | 视频一区国产 | 亚洲电影免费观看国语版 | 鲁丝片一区二区三区免费入口 | 黄色免费播放网站 |