Java Property类使用详解

网友投稿 182 2023-01-09


Java Property类使用详解

概念理解

Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。Properties类被许多java类使用。例如,在获取环境遍历时它就作为System.getProperties()方法的返回值。我们在很多需要避免硬编码的应用场景下需要使用Properties文件来加载程序需要配置的信息,比如JDBC、MyBatis框架等。Properties类则是Properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由Properties类。

写入

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

我们写一个类测试

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Date;

import java.util.Properties;

public class TestProperties {

public void writeProperties() {

Properties properties = new Properties();

OutputStream output = null;

try {

output = new FileOutputStream("config.properties");

properties.setProperty("url", "jdbc:mysql://localhost:3306/");

properties.setProperty("username", "root");

properties.setProperty("password", "root");

properties.setProperty("databases", "music_player");

properties.store(output, "Steven1997 modify" + new Date().toString());

} catch (IOException e) {

e.printStackTrace();

}finally {

if(output!=null) {

try {

output.close();

}catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

TestProperties t = new TestProperties();

t.writeProperties();

}

}

执行后,工程下面会出现一个config.properties文件,属性文件内容如下:

读取

使用getProperty获取config.properties文件配置文件的各项属性。

package property;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import javpZtpda.io.InputStream;

import java.util.Properties;

public class LoadProperties {

public void loadProperties() {

Properties properties = new Properties();

InputStream inputStream = null;

try {

inputStream = new FileInputStream("config.properties");

properties.load(inputStream);

System.out.println("url:" + properties.getProperty("url"));

System.out.println("username:" + properties.getProperty("username"));

System.out.println("password:" + properties.getProperty("password"));

System.out.println("database:" + properties.getProperty("database"));

} catch (IOException e) {

e.printStackTrace();

}finally {

if(inputStream !=null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

LoadProperties l = new LoadProperties();

l.loadProperties();

}

}

运行后的结果

url:jdbc:mysql://localhost:3306/

username:root

password:root

database:music_player

遍历

遍历属性文件中的键值对

package property;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.Map.Entry;

import java.util.Properties;

import java.util.Set;

public class PropertiesTest {

public void printAll() {

Properties prop = new Properties();

InputStream input = null;

try {

String file = "config.properties";

input = getClass().getClassLoader().getResourceAsStream(file);

if(input == null) {

System.out.println("无法加载文件" + file);

return ;

}

prop.load(input);

// 方法一

Set keys = prop.keySet();

for(Object key:keys) {

System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key));

}

//方法二:

Set> entrys = prop.entrySet();//返回的属性键值对实体

for(Entry entry:entrys){

System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());

}

//方法三:

Enumeration> e = prop.propertyNames();

while (e.hasMoreElements()) {

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

String value = prop.getProperty(key);

System.out.println("Key:" + key + ",Value:" + value);

}

}catch (Exception e) {

e.printStackTrace();

}finally {

if(input != null) {

try {

input.close();

}catch (Exception e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

PropertiesTest p = new PropertiesTest();

p.printAll();

}

}

运行结果如下:

key:url|value:jdbc:mysql://localhost:3306/

key:password|value:root

key:database|value:music_player

key:username|value:root

key:url,value:jdbc:mysql://localhost:3306/

key:password,value:root

key:database,value:music_player

key:upZtpdsername,value:root

Key:password,Value:root

Key:url,Value:jdbc:mysql://localhost:3306/

Key:database,Value:music_player

Key:username,Value:root

properties文件默认的编码格式居然是ISO-8859-1,这样导致往配置文件里面写入中文的时候转换成另一种格式的编码,需要把properties 文件的编码格式改为UTF-8,这样才会让配置文件保存中文数据的时候不会出现转码的问题

以上所述是给大家介绍的Java Property类使用详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解SpringBoot基础之banner玩法解析
下一篇:微服务网关需要单独部署吗(微服务为什么要用网关)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~