如果在命令行参数中指定,则覆盖属性文件值 [英] Overwrite property file values if specified in the command line arguments

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

问题描述

我正在一个项目中,我需要从 config.properties 文件中读取所有内容。以下是我的 config.properties 文件-

I am working on a project in which I need to read everything from config.properties file. Below is my config.properties file-

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

而且我正在像这样从命令提示符处运行我的程序-并且它工作正常。

And I am running my program from the command prompt like this- And it is working fine.

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

下面是我的程序-

private static Properties prop = new Properties();

private static int noOfThreads;
private static int noOfTasks;
private static int startRange;
private static String location;
private static List<String> tableNames = new ArrayList<String>();

public static void main(String[] args) {

        location = args[0];

        try {

            readPropertyFiles();

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

    private static void readPropertyFiles() throws FileNotFoundException, IOException {

        prop.load(new FileInputStream(location));

        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) {

            //Other Code
        }
    }

问题陈述:-

我现在要做什么我想做的是-如果我要传递其他参数,例如 NUMBER_OF_THREADS,NUMBER_OF_TASKS,ID_START_RANGE,TABLES config.properties,请从命令提示符处文件,则应覆盖 config.properties文件的值。因此,如果我正在运行这样的程序-

Now what I am trying to do is- Suppose from the command prompt if I am passing other arguments such as NUMBER_OF_THREADS, NUMBER_OF_TASKS, ID_START_RANGE, TABLES along with config.properties file, then it should overwrite the values of config.properties file. So if I am running my program like this-

java -jar Test.jar C:\\test\\config .properties t:10 n:100 i:2 TABLES:TABLE1 TABLES:TABLE2 TABLES:TABLE3

然后在我的程序中

noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000
startRange should be 2 instead of 1
tableNames should have three table TABLE1, TABLE2, TABLE3 instead of TABLE1 and TABLE2.

以上格式如果需要覆盖config.property文件,将按照以下格式进行操作。

Above format I will follow if I need to overwrite the config.property file.

但是如果我这样运行-

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

然后应从 config.properties文件读取所有内容

通常,如果我通过,我想覆盖 config.properties文件命令行中的参数以及 config.property 文件位置。

In general I want to overwrite config.properties file if I am passing arguments in the command line along with config.property file location.

任何人都可以提供示例给我(干净的方法)做这种情况?

Can anyone provide me an example (clean way) of doing this scenario?

推荐答案

您可以手动合并它们,但是您需要在命令行选项中使用上下文,您如何知道应该添加TABLE3到tableNames数组,而不是10、100和2?

You can manually merge them, but you need context on the command line options, how do you know that TABLE3 should be added to the tableNames array, but not 10, 100 and 2?

如果要像这样更改命令行:

If you were to change the command line like so:

java -jar Test.jar "C:\\test\\config.properties" 10 100 2 TABLES:TABLE1 TABLES:TABLE2 TABLES:TABLE3

然后,在读取属性文件后,您可以循环遍历main方法中的命令行参数,并插入或添加属性条目。

Then you could cycle through the command line arguments in your main method after you have done a read of the property file, and insert or add property entries.

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

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