config.properties
コマンドラインに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"
ファイルから 4 つのプロパティをすべて読み取る必要がありconfig.properties
ます。しかし、このようにプログラムを実行しているとします-
java -jar Test.jar "C:\\test\\config.properties" 10 100 2 TABLE1 TABLE2 TABLE3
次に、引数からすべてのプロパティを読み取り、config.properties ファイル内のプロパティを上書きする必要があります。
以下は、このシナリオで正常に動作している私のコードです-
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
}
}
問題文:-
今私がやろうとしていることは、誰かがこのようなプログラムを実行しているとします
java -jar Test.jar "C:\\test\\config.properties" 10
noOfThreads
次に、私のプログラムでは、上書きする必要があります-
noOfThreads should be 10 instead of 100
そして、その人がこのようなプログラムを実行しているとします-
java -jar Test.jar "C:\\test\\config.properties" 10 100
次に、私のプログラムでは、上書きする必要がありますnoOfThreads
-noOfTasks
noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000
また、他のユースケースも考えられます。
このシナリオを達成する方法を誰かに提案できますか? 助けてくれてありがとう