Overwrite property file values if specified in the

2019-09-09 17:53发布

问题:

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"

Below is my program-

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
        }
    }

Problem Statement:-

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

then in my program-

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.

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

But if I am running like this-

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

then it should read everything from the config.properties file.

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?

回答1:

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

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.