Java 对 Properties 文件的操作详解及简单实例

2025-05-29 0 69

Java 对 Properties 文件的操作

简介

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java 对 Properties 文件的操作详解及简单实例 https://www.kuaiidc.com/118902.html

相关文章

发表评论
暂无评论