java中读写Properties属性文件公用方法详解

2025-05-29 0 35

前言

大家都知道Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

它提供了几个主要的方法:

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 () ,清除所有装载的 键 – 值对。该方法在基类中提供。

如下示例代码提供了一套读写配置文件的公用实用方法,可以根据自己的项目进行引入:

?

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
package com.javacui.lucene.jdbc;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertieUtil {

private static Logger logger = Logger.getLogger(PropertieUtil.class);

private PropertieUtil() {

}

/**

* 读取配置文件某属性

*/

public static String readValue(String filePath, String key) {

Properties props = new Properties();

try {

// 注意路径以 / 开始,没有则处理

if (!filePath.startsWith("/"))

filePath = "/" + filePath;

InputStream in = PropertieUtil.class.getResourceAsStream(filePath);

props.load(in);

String value = props.getProperty(key);

return value;

} catch (Exception e) {

logger.error(e);

return null;

}

}

/**

* 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)

*/

public static void readProperties(String filePath) {

Properties props = new Properties();

try {

// 注意路径以 / 开始,没有则处理

if (!filePath.startsWith("/"))

filePath = "/" + filePath;

InputStream in = PropertieUtil.class.getResourceAsStream(filePath);

props.load(in);

Enumeration<?> en = props.propertyNames();

// 遍历打印

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String Property = props.getProperty(key);

logger.info(key + ":" + Property);

}

} catch (Exception e) {

logger.error(e);

}

}

/**

* 将值写入配置文件

*/

public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {

// 本地测试特别注意,如果是maven项目,请到\\target目录下查看文件,而不是源代码下

// 注意路径不能加 / 了,加了则移除掉

if (fileName.startsWith("/"))

fileName.substring(1);

String filePath = PropertieUtil.class.getResource("/").getPath()+fileName;

// 获取配置文件

Properties pps = new Properties();

InputStream in = new BufferedInputStream(new FileInputStream(filePath));

pps.load(in);

in.close();

OutputStream out = new FileOutputStream(filePath);

// 设置配置名称和值

pps.setProperty(parameterName, parameterValue);

// comments 等于配置文件的注释

pps.store(out, "Update " + parameterName + " name");

out.flush();

out.close();

}

public static void main(String[] args) throws Exception {

readProperties("jdbc.properties");

// logger.info(readValue("jdbc.properties", "JAVABLOG_WRITE_URL"));

// writeProperties("test.properties", "test", "test");

}

}

总结

以上就是关于java读写Properties属性文件公用方法的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 java中读写Properties属性文件公用方法详解 https://www.kuaiidc.com/119465.html

相关文章

发表评论
暂无评论