Java.util.Properties类
·
在 Java 中,java.util.Properties 是一个专门用于处理 属性文件(.properties) 的工具类,它继承自 Hashtable<Object, Object>,主要用于读取和写入以键值对(key=value)形式存储的配置信息。
核心作用
属性文件(.properties)是 Java 中常用的配置文件格式(如数据库连接信息、系统参数等),Properties 类提供了便捷的方法来:
- 从文件、输入流中加载配置(
load()方法); - 向文件、输出流中写入配置(
store()方法); - 根据键获取对应的值(
getProperty()方法); - 设置键值对(
setProperty()方法)。
常用方法
| 方法 | 说明 |
|---|---|
void load(InputStream in) |
从输入流(如文件流)加载属性配置 |
void store(OutputStream out, String comments) |
将属性配置写入输出流(如文件),comments 为注释 |
String getProperty(String key) |
根据键获取值,若不存在则返回 null |
String getProperty(String key, String defaultValue) |
根据键获取值,若不存在则返回默认值 |
Object setProperty(String key, String value) |
设置键值对(键和值均为字符串) |
Enumeration<?> propertyNames() |
获取所有键的枚举(遍历用) |
使用示例
假设存在一个 config.properties 文件,内容如下:
properties
# 数据库配置
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=123456
1. 读取属性文件
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
// 加载配置文件
props.load(fis);
// 获取配置值
String url = props.getProperty("db.url");
String username = props.getProperty("db.username");
String password = props.getProperty("db.password");
// 获取不存在的键时返回默认值
String driver = props.getProperty("db.driver", "com.mysql.cj.jdbc.Driver");
System.out.println("URL: " + url);
System.out.println("Username: " + username);
System.out.println("Driver: " + driver); // 输出默认值
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 写入属性文件
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesWriteDemo {
public static void main(String[] args) {
Properties props = new Properties();
try (FileOutputStream fos = new FileOutputStream("new_config.properties")) {
// 设置键值对
props.setProperty("app.name", "MyApp");
props.setProperty("app.version", "1.0.0");
// 写入文件(注释会被添加到文件头部)
props.store(fos, "Application Configuration");
System.out.println("配置已写入");
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行后会生成 new_config.properties,内容如下:
properties
#Application Configuration
#Wed Oct 15 15:30:00 CST 2025
app.name=MyApp
app.version=1.0.0
注意事项
- 编码问题:
load()方法默认使用 ISO-8859-1 编码读取文件,若文件包含中文,可能出现乱码。解决方式:- 用
load(Reader)方法(如InputStreamReader)指定编码(如 UTF-8); - 或在 Java 9+ 中使用
load(InputStream, Charset)直接指定编码。
- 用
- 键值对格式:属性文件中
key=value可以用=或:分隔,空格会被忽略;注释以#或!开头。 - 继承自 Hashtable:
Properties是线程安全的(因为继承自Hashtable),但如果不需要线程安全,可考虑使用更轻量的HashMap替代(需自行处理文件读写)。
典型应用场景
- 读取数据库连接配置、日志配置等系统参数;
- 存储国际化(i18n)资源(如多语言文本);
- 保存程序运行时的动态配置(如用户偏好设置)。
Properties 类是 Java 处理配置文件的基础工具,简单易用,广泛应用于各类 Java 程序中。
补充:
在 IntelliJ 中使用 Properties 类的正确姿势
1. 普通 Java 项目(非 Maven/Gradle)
可以直接在项目中放 .properties 文件,只要路径正确,Properties 就能读取,无需刻意创建 resource 文件夹。例如:
- 在项目根目录下创建
config.properties:properties
name=test age=18 - 读取代码(使用绝对路径或相对路径):
import java.io.FileInputStream; import java.util.Properties; public class Test { public static void main(String[] args) throws Exception { Properties props = new Properties(); // 相对路径(相对于项目运行目录) props.load(new FileInputStream("config.properties")); System.out.println(props.getProperty("name")); // 输出 test } }
2. Maven/Gradle 项目(推荐用 resource 目录)
Maven/Gradle 项目约定 src/main/resources 为资源目录,该目录下的文件会被自动复制到编译后的 classpath(即 target/classes 或 build/classes 目录)中,方便通过类路径读取(无需关心绝对路径)。
步骤:
- 在
src/main/resources下创建config.properties(IntelliJ 会自动识别该目录为资源目录,图标带文件夹 + 小地球)。 - 读取代码(通过类路径读取,更灵活,适合打包部署):
import java.io.InputStream; import java.util.Properties; public class Test { public static void main(String[] args) throws Exception { Properties props = new Properties(); // 从 classpath 中读取(推荐,无需关心具体路径) InputStream is = Test.class.getClassLoader().getResourceAsStream("config.properties"); props.load(is); System.out.println(props.getProperty("name")); } }
三、什么是 ResourceBundle?
ResourceBundle 是 Java 专门用于国际化(多语言) 的工具类,它能根据系统语言环境(如中文、英文)自动加载对应的 .properties 文件。
例如:
- 创建多语言配置文件(放在
resource目录下):message_en.properties(英文):greeting=Hellomessage_zh.properties(中文):greeting=你好
- 使用
ResourceBundle自动匹配语言:import java.util.Locale; import java.util.ResourceBundle; public class I18nTest { public static void main(String[] args) { // 加载中文环境的资源(会自动找 message_zh.properties) ResourceBundle bundle = ResourceBundle.getBundle("message", Locale.CHINA); System.out.println(bundle.getString("greeting")); // 输出 你好 // 加载英文环境的资源 ResourceBundle enBundle = ResourceBundle.getBundle("message", Locale.US); System.out.println(enBundle.getString("greeting")); // 输出 Hello } }
ResourceBundle 本质是对 Properties 的封装,专为多语言场景设计,而普通配置读取用 Properties 即可。
更多推荐


所有评论(0)