如果存在命令行值,则覆盖属性文件 [英] Overwrite the properties file if command line value is present

查看:137
本文介绍了如果存在命令行值,则覆盖属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果命令行除config.properties文件位置之外不包含任何参数,我有一个程序将从 config.properties 文件中读取所有内容。下面是我的config.properties文件 -

I have a program which will read everything from config.properties file if command line does not contain any arguments apart from config.properties file location. Below is my config.properties file-

NUMBER_OF_THREADS: 100
NUMBER_OF_TASKS: 10000
ID_START_RANGE: 1
TABLES: TABLE1,TABLE2

如果我从命令提示符运行我的程序,就像这样 -

If I am running my program from the command prompt like this-

java -jar Test.jarC:\\test\\config.properties

它应该读取 config.properties 文件中的所有四个属性。但是假设我运行我的程序是这样的 -

It should read all the four properties from the config.properties file. But suppose if I am running my program like this-

java -jar Test.jarC:\\test\\ config.properties10 100 2 TABLE1 TABLE2 TABLE3

然后它应该读取参数中的所有属性并覆盖config.properties中的属性文件。

then it should read all the properties from the arguments and overwrite the properties in config.properties file.

以下是我的代码,在这种情况下工作正常 -

Below is my code which is working fine in this scenario-

public static void main(String[] args) {

        try {

            readPropertyFiles(args);

        } catch (Exception e) {
            LOG.error("Threw a Exception in" + CNAME + e);
        }
    }

    private static void readPropertyFiles(String[] args) throws FileNotFoundException, IOException {

        location = args[0];

        prop.load(new FileInputStream(location));

        if(args.length >= 1) {
            noOfThreads = Integer.parseInt(args[1]);
            noOfTasks = Integer.parseInt(args[2]);
            startRange = Integer.parseInt(args[3]);

            tableName = new String[args.length - 4];
            for (int i = 0; i < tableName.length; i++) {
                tableName[i] = args[i + 4];
                tableNames.add(tableName[i]);
            }
        } else {
            noOfThreads = Integer.parseInt(prop.getProperty("NUMBER_OF_THREADS").trim());
            noOfTasks = Integer.parseInt(prop.getProperty("NUMBER_OF_TASKS").trim());
            startRange = Integer.parseInt(prop.getProperty("ID_START_RANGE").trim());
            tableNames = Arrays.asList(prop.getProperty("TABLES").trim().split(","));
        }

        for (String arg : tableNames) {

            //Some Other Code

        }
    }   

问题陈述: -

现在我要做的是 - 假设任何人正在运行这样的程序

Now what I am trying to do is- Suppose if any person is running program like this

java -jar Test.jarC :\\test\\config.properties10

然后在我的程序中,它应该覆盖 noOfThreads only -

then in my program, it should overwrite noOfThreads only-

noOfThreads should be 10 instead of 100

并假设该人正在运行这样的程序 -

And suppose if that person is running program like this-

java -jar Test.jarC:\\test\\config.properties10 100

然后在我的程序中,它应该只覆盖 noOfThreads noOfTasks -

then in my program, it should overwrite noOfThreads and noOfTasks only-

noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000

以及其他可能的用例。

And possible other use cases as well.

有谁能建议我如何实现这种情况?感谢您的帮助

Can anyone suggest me how to achieve this scenario? Thanks for the help

推荐答案

创建一个循环。

List<String> paramNames = new ArrayList<String>{"NUMBER_OF_THREADS", "NUMBER_OF_TASKS", 
            "ID_START_RANGE", "TABLES"}; // Try to reuse the names from the property file
Map<String, String> paramMap = new HashMap<String, String>();
...
// Validate the length of args here
...
// As you table names can be passed separately. You need to handle that somehow. 
// This implementation would work when number of args will be equal to number of param names
for(int i = 0; i< args.length; i++) {
   paramMap.put(paramNames[i], args[i]); 
}

props.putAll(paramMap);
... // Here props should have it's values overridden with the ones provided

这篇关于如果存在命令行值,则覆盖属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆