我正在做一个项目,我需要从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, TABLES
连同config.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
文件位置,我想覆盖。
谁能给我一个做这个场景的例子(干净的方式)?