如何使用java加载和读取多个属性文件

问题描述 投票:-1回答:2

我有3个属性文件,如web1.propertiesweb2.propertiesweb3.properties。我想使用单个属性对象一次加载所有属性文件。

Properties prop = new Properties();
prop.load("web1.properties");
prop.load("web2.properties");
prop.load("web3.properties");`

我正在尝试下面的代码来加载多个属性文件。

public class ReadFileExtension {
    static Properties prop = new Properties();
    public static void main(String[] args) throws IOException {
        String projectFolder = System.getProperty("user.dir");
        File f1 = new File(projectFolder);
        File[] listOfFiles = f1.listFiles();
        System.out.println("Length:"+listOfFiles.length);
        for(int i=0;i<listOfFiles.length;i++){
            if (listOfFiles[i].isDirectory()) {
                //System.out.println("File " + listOfFiles[i].getAbsolutePath());
                String prop1 = listOfFiles[i].getAbsolutePath();
                if(prop1.contains("properties")){
                    System.out.println(prop1);

                    File folder = new File(prop1);
                    File[] listOfFiles1 = folder.listFiles();

                        for (int j = 0; j < listOfFiles1.length; j++) {
                          if (listOfFiles1[j].isFile()) {
                            //System.out.println("Feature File: " + listOfFiles1[j].getAbsolutePath());
                            //prop.load(new FileInputStream(listOfFiles1[j].getAbsolutePath()));
                            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(listOfFiles1[j].getAbsolutePath()));
                          } 
                        }
                }
              } 
        }
        System.out.println("Prop 1: "+prop.getProperty("ApplicationURL"));
        System.out.println("Prop 2: "+prop.getProperty("Application"));
    }
}
java selenium selenium-webdriver
2个回答
1
投票

我认为Abhishek提供的链接可以帮助你。

我已经在我的本地机器上测试过了,对我来说效果很好。

码:

public class MultiplePropertyLoader {

    private static Properties properties;

    public static void main(String[] args) {

        try {
            properties = new Properties();
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey.properties"));
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey1.properties"));
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey2.properties"));

            System.out.println("::: Property File 1 Data :::");
            System.out.println(properties.getProperty("ENCRYPTION_KEY"));
            System.out.println(properties.getProperty("USERNAME"));
            System.out.println(properties.getProperty("PASSWORD"));

            System.out.println("::: Property File 2 Data :::");
            System.out.println(properties.getProperty("ENCRYPTION_KEY1"));
            System.out.println(properties.getProperty("USERNAME1"));
            System.out.println(properties.getProperty("PASSWORD1"));

            System.out.println("::: Property File 3 Data :::");
            System.out.println(properties.getProperty("ENCRYPTION_KEY2"));
            System.out.println(properties.getProperty("USERNAME2"));
            System.out.println(properties.getProperty("PASSWORD2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果不起作用,请发帖给我。

这是一个运行代码和示例数据的my git repository URL。您可以克隆存储库并在本地计算机上运行,​​它将起作用。


0
投票

您可以使用此语法加载多个属性。

Properties properties = new Properties();

properties.load(new FileInputStream("web1.properties"));

Properties properties2 = new Properties();
properties2.load(new FileInputStream("web2.properties"));

properties.putAll(properties2);

阅读属性是一样的。

© www.soinside.com 2019 - 2024. All rights reserved.