. 使用Properties类(适用于配置文件)如果你需要处理配置文件(如.properties文件),可以使用Properties类,它继承自Hashtable并实现了Map接口。
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
public class PropertiesExample {
public static void main(String[] args) {
Properties prop = new Properties();
try {
// 加载配置文件
prop.load(new FileInputStream("config.properties"));
// 获取配置项的值
String value = prop.getProperty("someKey");
System.out.println("Value: " + value);
} catch (IOException e) {
e.printStackTrace();
}
}
}