
Java的java.util.Properties类是处理.properties配置文件的标准工具。它提供了一种键值对的存储方式,其中键(key)和值(value)都是字符串。通常,我们使用getProperty(String key)方法来根据键获取对应的值。然而,这个方法要求传入的键与文件中的键完全匹配。
在某些业务场景中,一个配置项的键可能由多个部分组成,例如VN1234:1234。如果需求变更,我们希望能够仅仅通过键的一部分(例如1234)来查找对应的值,getProperty()方法就无法满足要求,因为它会认为1234与VN1234:1234是不同的键。直接修改或删除原键的一部分通常是不允许或不推荐的,因为它会改变原有的配置结构。
当无法进行精确键匹配时,一种有效的策略是获取Properties对象中所有的键,然后对这些键进行遍历,并使用字符串匹配方法(如contains())来查找包含目标子串的键。一旦找到匹配的键,就可以通过该完整键来获取对应的值。
以下代码演示了如何加载一个properties文件,并根据键的子串来查找对应的值:
立即学习“Java免费学习笔记(深入)”;
首先,假设我们有一个名为config.properties的文件,内容如下:
VN1234:1234 = A ABC:5678 = B XYZ:12345 = C
现在,我们希望通过子串1234来获取值A。
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PartialKeySearch {
    public static void main(String[] args) {
        Properties properties = new Properties();
        String filePath = "config.properties"; // 确保config.properties文件在项目根目录或指定路径
        try (FileInputStream fis = new FileInputStream(filePath)) {
            properties.load(fis);
            System.out.println("成功加载配置文件: " + filePath);
            String searchPartialKey = "1234";
            String foundKey = null;
            String foundValue = null;
            // 获取所有属性键的集合
            Set<String> propertyNames = properties.stringPropertyNames();
            System.out.println("\n开始遍历键并查找包含 '" + searchPartialKey + "' 的键...");
            for (String key : propertyNames) {
                if (key.contains(searchPartialKey)) {
                    foundKey = key;
                    foundValue = properties.getProperty(key);
                    System.out.println("找到匹配的键: " + foundKey + ", 对应的值: " + foundValue);
                    // 如果只需要找到第一个匹配项,可以在此处break;
                    // break; 
                }
            }
            if (foundKey == null) {
                System.out.println("未找到包含 '" + searchPartialKey + "' 的任何键。");
            }
            // 示例:查找另一个子串
            System.out.println("\n尝试查找包含 '5678' 的键...");
            searchPartialKey = "5678";
            foundKey = null;
            foundValue = null;
            for (String key : propertyNames) {
                if (key.contains(searchPartialKey)) {
                    foundKey = key;
                    foundValue = properties.getProperty(key);
                    System.out.println("找到匹配的键: " + foundKey + ", 对应的值: " + foundValue);
                    break; 
                }
            }
        } catch (IOException e) {
            System.err.println("加载配置文件时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}运行上述代码,预期输出如下:
成功加载配置文件: config.properties 开始遍历键并查找包含 '1234' 的键... 找到匹配的键: VN1234:1234, 对应的值: A 尝试查找包含 '5678' 的键... 找到匹配的键: ABC:5678, 对应的值: B
通过遍历Properties对象的stringPropertyNames()方法获取所有键,并结合字符串的contains()方法进行子串匹配,可以有效地解决Properties文件无法直接通过部分键名获取值的限制。这种方法提供了更高的灵活性,适用于键名结构复杂或查找需求不完全匹配的场景。在实际应用中,应根据配置文件的规模和查找频率,综合考虑性能、匹配精度和多重匹配的处理策略,选择最适合的实现方式。
以上就是Java Properties文件:通过部分键名灵活获取配置值的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                
                                
                                
                                
                                
                                
                                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号