0

私はconfig.propertiesファイルからすべてを読み取る必要があるプロジェクトに取り組んでいます。以下は私のconfig.propertiesファイルです-

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

そして、私はこのようにコマンドプロンプトからプログラムを実行しています-そしてそれは正常に動作しています。

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, TABLESconfig.properties fileますconfig.properties file。したがって、このようにプログラムを実行している場合-

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ファイルを上書きする必要がある場合に従います。

しかし、私がこのように走っているなら-

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

次に、からすべてを読み取る必要がありますconfig.properties file

一般に、コマンドラインでファイルの場所config.properties fileとともに引数を渡す場合は上書きしたいと思います。config.property

誰かが私にこのシナリオを行う例(クリーンな方法)を提供できますか?

4

1 に答える 1

2

それらを手動でマージすることはできますが、コマンドラインオプションのコンテキストが必要です。TABLE3をtableNames配列に追加する必要があるが、10、100、2は追加しないことをどのように知っていますか?

次のようにコマンドラインを変更する場合:

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

次に、プロパティファイルの読み取りを行った後、メインメソッドのコマンドライン引数を循環し、プロパティエントリを挿入または追加できます。

于 2013-03-18T01:57:41.987 に答える