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));

}

}

输出结果都是

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在IDEA中配置properties文件有以下几个步骤: 1. 创建一个Properties的实例,并设置需要的键值对。可以使用setProperty()方法来设置属性的键和值。 2. 使用FileWriterProperties对象中的属性存储到文件中。可以使用store()方法将属性写入到文件中,需要提供一个FileWriter对象和一个注释。 3. 使用FileReader从文件中读取Properties对象的属性。可以使用load()方法从文件中加载属性,需要提供一个FileReader对象。 4. 在IDEA中配置properties文件,可以按照以下步骤进行操作: - 在项目中创建一个properties文件,可以选择Resource Bundle型的文件。 - 将配置信息以文本文件的格式写入到properties文件中。 综上所述,通过以上方法可以在IDEA中配置properties文件。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Properties简介 及 IDEA properties文件配置方法](https://blog.csdn.net/weixin_40901329/article/details/79845282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [IDEA使用properties配置文件进行mysql数据库连接的教程图解](https://download.csdn.net/download/weixin_38682406/12755617)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值