Java Properties 读取指南
在Java中,Properties类用于读取和写入属性文件(.properties文件),这些文件通常用于存储配置信息,以下是几种读取properties文件的常用方法:

(图片来源网络,侵删)
基本读取方法
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesReader {
public static void main(String[] args) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream("config.properties")) {
// 加载属性文件
prop.load(input);
// 读取属性值
String dbUrl = prop.getProperty("database.url");
String username = prop.getProperty("database.username");
String password = prop.getProperty("database.password");
System.out.println("Database URL: " + dbUrl);
System.out.println("Username: " + username);
System.out.println("Password: " + password);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
从类路径读取
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ClasspathPropertiesReader {
public static void main(String[] args) {
Properties prop = new Properties();
try (InputStream input = ClasspathPropertiesReader.class.getClassLoader()
.getResourceAsStream("config.properties")) {
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
prop.load(input);
// 读取属性值
System.out.println(prop.getProperty("app.name"));
System.out.println(prop.getProperty("app.version"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
设置默认值
Properties prop = new Properties();
prop.load(input);
// 读取属性值,如果不存在则返回默认值
String timeout = prop.getProperty("connection.timeout", "30");
读取所有属性
// 遍历所有属性
for (String key : prop.stringPropertyNames()) {
String value = prop.getProperty(key);
System.out.println(key + " => " + value);
}
使用ResourceBundle(适合国际化)
import java.util.ResourceBundle;
public ResourceBundleExample {
public static void main(String[] args) {
// 不需要扩展名
ResourceBundle rb = ResourceBundle.getBundle("messages");
System.out.println(rb.getString("welcome.message"));
System.out.println(rb.getString("goodbye.message"));
}
}
Spring Boot中的属性读取
在Spring Boot中,可以使用@Value注解:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
// getters...
}
或者使用@ConfigurationProperties:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "database")
public class DatabaseConfig {
private String url;
private String username;
private String password;
// getters and setters...
}
常见问题解决
- 文件路径问题:确保文件路径正确,特别是从类路径读取时
- 编码问题:如果properties文件包含非ASCII字符,指定编码:
try (InputStreamReader reader = new InputStreamReader( new FileInputStream("config.properties"), "UTF-8")) { prop.load(reader); } - 属性值包含特殊字符:使用反斜杠转义或Unicode表示
示例properties文件内容
# Database configuration
database.url=jdbc:mysql://localhost:3306/mydb
database.username=admin
database.password=secret
# Application settings
app.name=My Application
app.version=1.0.0
connection.timeout=30
(图片来源网络,侵删)
