Properties类简介

本文详细介绍了Java Properties类的使用方法,包括设置键值对、读取配置、遍历属性及持久化到文件。通过六种方式展示了如何加载和操作src/main/resources/prop.properties文件,适合初学者和开发者参考。

Properties类

下面我们从写入、读取、遍历等角度来解析Properties类的常见用法

项目路径如下

src

main

java

com

jourwon

prop

PropertiesTest

resources

config.properties

prop.properties

PropertiesTest为测试类,prop.properties文件的内容如下

username=root

password=123456

[](()写入

Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

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

Properties properties = new Properties();

OutputStream output = null;

try {

output = new FileOutputStream(“src/main/resources/config.properties”);

properties.setProperty(“username”, “root”);

properties.setProperty(“password”, “123456”);

// 保存键值对到文件中

properties.store(output, “JourWon modify”);

} catch (IOException io) {

io.printStackTrace();

} finally {

if (output != null) {

try {

output.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

输出结果,在resources目录下多一个文件config.properties,内容如下

#JourWon modify

#Mon Sep 09 14:23:44 CST 2019

password=123456

username=root

[](()读取

下面给出常见的六种读取properties文件的方式:

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

// PropertiesTest.getPath1();

// PropertiesTest.getPath2();

// PropertiesTest.getPath3();

// PropertiesTest.getPath4();

// PropertiesTest.getPath5();

PropertiesTest.getPath6();

}

/**

  • 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件

  • 主要是需要加上src这个文件夹名。路径配置需要精确到绝对地址级别

  • @return

*/

public static void getPath1() throws IOException {

InputStream in = new BufferedInputStream(new FileInputStream(

new File(“src/main/resources/prop.properties”)));

printKeyValue(in);

}

/**

  • 二、 使用java.util.ResourceBundle类的getBundle()方法

  • 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,注意不需要带上后缀名。

  • @return

*/

public static void getPath2() {

ResourceBundle rb = ResourceBundle

.getBundle(“prop”);

printKeyValueRb(rb);

}

/**

  • 三、 使用java.util.PropertyResourceBundle类的构造函数

  • @return

*/

public static void getPath3() throws IOException {

InputStream in = new BufferedInputStream(new FileInputStream(“src/main/resources/prop.properties”));

ResourceBundle rb = new PropertyResourceBundle(in);

printKeyValueRb(rb);

}

/**

  • 四、 使用class变量的getResourceAsStream()方法

  • 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀

  • @return

*/

public static void getPath4() throws IOException {

InputStream in = PropertiesTest.class

.getResourceAsStream("/prop.properties");

printKeyValue(in);

}

/**

  • 五、

  • 使用class.getClassLoader()所得到的java.lang.ClassLoader的

  • getResourceAsStream()方法

  • getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀

  • 否则会报空指针异常

  • @return

*/

public static void getPath5() throws IOException {

InputStream in = PropertiesTest.class.getClassLoader()

.getResourceAsStream("./././prop.properties");

printKeyValue(in);

}

/**

  • 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

  • getSystemResourceAsStream()方法的参数格式也是有固定要求的

  • @return

*/

public static void getPath6() throws IOException {

InputStream in = ClassLoader

.getSystemResourceAsStream("./././prop.properties");

printKeyValue(in);

}

/**

  • 单独抽取的方法,用户检测能否正确操纵Properties

  • @param inputStream

  • @throws IOException

*/

public static void printKeyValue(InputStream inputStream) throws IOException {

Properties properties = new Properties();

properties.load(inputStream);

Set keys = properties.keySet();

for (Object key : keys) {

System.out.println(key + " = " + properties.get(key));

}

if (inputStream != null) {

inputStream.close();

}

}

public static void printKeyValueRb(ResourceBundle rb) {

Set keys = rb.keySet();

for (String key : keys) {

System.out.println(key + " = " + rb.getString(key));

}

}

输出结果都是

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值