通常,Java属性文件用于存储项目配置数据或设置。 在本教程中,我们将向您展示如何读写.properties
文件。
Properties prop = new Properties();
// set key and value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "mkyong");
prop.setProperty("db.password", "password");
// save a properties file
prop.store(outputStream, "");
// load a properties file
prop.load(inputStream)
// get value by key
prop.getProperty("db.url");
prop.getProperty("db.user");
prop.getProperty("db.password");
// get all keys
prop.keySet();
// print everything
prop.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));
用于测试的简单Maven项目结构。
1.写入属性文件
设置属性键和值,并将其保存在某处。
App1.java
package com.mkyong;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class App1 {
public static void main(String[] args) {
try (OutputStream output = new FileOutputStream("path/to/config.properties")) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "mkyong");
prop.setProperty("db.password", "password");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (IOException io) {
io.printStackTrace();
}
}
}
输出量
{db.user=mkyong, db.password=password, db.url=localhost}
创建path/to/config.properties
。
path/to/config.properties
#Thu Apr 11 17:37:58 SRET 2019
db.user=mkyong
db.password=password
db.url=localhost
2.加载属性文件
从文件系统加载属性文件并检索属性值。
App2.java
package com.mkyong;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App2 {
public static void main(String[] args) {
try (InputStream input = new FileInputStream("path/to/config.properties")) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
输出量
localhost
mkyong
password
3.从类路径加载属性文件
从项目类路径加载属性文件config.properties
,并检索属性值。
src/main/resources/config.properties
db.url=localhost
db.user=mkyong
db.password=password
App3.java
package com.mkyong;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App3 {
public static void main(String[] args) {
try (InputStream input = App3.class.getClassLoader().getResourceAsStream("config.properties")) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
//load a properties file from class path, inside static method
prop.load(input);
//get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
输出量
localhost
mkyong
password
4.打印属性文件中的所有内容
从项目类路径加载属性文件config.properties
,并打印出键和值。
App4.java
package com.mkyong;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class App4 {
public static void main(String[] args) {
App4 app = new App4();
app.printAll("config.properties");
}
private void printAll(String filename) {
try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename)) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
prop.load(input);
// Java 8 , print key and values
prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value));
// Get all keys
prop.keySet().forEach(x -> System.out.println(x));
Set<Object> objects = prop.keySet();
/*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 (IOException ex) {
ex.printStackTrace();
}
}
}
输出量
Key : db.user, Value : mkyong
Key : db.password, Value : password
Key : db.url, Value : localhost
db.user
db.password
db.url
下载源代码
下载– java-properties-file.zip (6KB)