软件编程
位置:首页>> 软件编程>> java编程>> Java Property类使用详解

Java Property类使用详解

作者:CS-Bob  发布时间:2023-11-06 21:52:57 

标签: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文件,属性文件内容如下:

Java Property类使用详解

读取

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


package property;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.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<Object> keys = prop.keySet();
for(Object key:keys) {
System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key));
}
//方法二:
Set<Entry<Object, Object>> entrys =prop.entrySet();//返回的属性键值对实体
for(Entry<Object, Object> 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:username,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类使用详解整合,希望对大家有所帮助。

来源:https://blog.csdn.net/bo123_/article/details/89059799

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com