我有一个程序,它将读一切从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"
它应全部来自config.properties
文件中的四个属性。但是假设,如果我运行一个像这个 -
java -jar Test.jar "C:\\test\\config.properties" 10 100 2 TABLE1 TABLE2 TABLE3
我的程序,那么它应该读取参数的所有属性和覆盖在config.properties文件的属性。
下面是我的代码,在此scenario-
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
only-
noOfThreads should be 10 instead of 100
再假设,如果该人正在运行的程序像这个 -
java -jar Test.jar "C:\\test\\config.properties" 10 100
然后在我的程序,它应该覆盖noOfThreads
和noOfTasks
only-
noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000
还有可能的其他用例。
任何人都可以建议我如何实现这一方案?感谢您的帮助
ASK Adeel。感谢Adeel的帮助。我不使用JDK1.7,因此不能使用这些大括号。 :(而且还可以提供你我的全部流程在那里我应该把这个做这件事的工作感谢您的帮助 – ferhan 2013-03-18 04:10:45
TechGeeky:我已经取代钻石商,这样你就可以使用Java 6 – 2013-03-19 01:49:51